Solved

Casting in Java, why?

Posted on 2013-05-19
3
396 Views
Last Modified: 2013-05-20
Hello Experts,

This is what I expect to be a 'noob' question, but I cannot figure out why I need to Cast this object.  I have a button in my Android app and apparently have to Cast the object...why?  Code below:
Button b = (Button) findViewById(R.id.button1);

Open in new window

FYI...I'm following a tutorial on Lynda.com and this was mentioned as important.  I would really appreciate any help to understand 'why' it needs to be Cast as a button when it IS a button already and b was type cast as a button too.

Thank you.
0
Comment
Question by:Modifier1000
3 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 39180229
though the actual view is a button, the return type of findViewById is different than Button.
0
 
LVL 7

Accepted Solution

by:
Beneford earned 500 total points
ID: 39180235
findViewById() is actually returning a View object (a Button is a subclass of TextView which is a subclass of View)
You know that the View returned is a Button, but the Java sysem can't work that out just by looking at the source code. The type check system tries to stop you doing things that aren't going to work (very useful most of the time), and the cast is how you tell the type check system that you really do know what's going on.
0
 

Author Closing Comment

by:Modifier1000
ID: 39181111
Thank you for the explanation, very helpful in giving more depth to your answer.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now