Link to home
Create AccountLog in
Avatar of Modifier1000
Modifier1000Flag for United States of America

asked on

Casting in Java, why?

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.
Avatar of ksivananth
ksivananth
Flag of United States of America image

though the actual view is a button, the return type of findViewById is different than Button.
ASKER CERTIFIED SOLUTION
Avatar of Beneford
Beneford
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Modifier1000

ASKER

Thank you for the explanation, very helpful in giving more depth to your answer.