Welcome to the Treehouse Community
Looking to learn something new?
Arthur Podkowiak3,633 Points
Why cast showFactButton to a Button?
Why do we have to cast the showFactButton to be a button? For the
factLabel we cast the method to find the id for the View to TextView because that's how we created our object, factLabel. But for the button, showFactButton, we initialized it as an object in the Button class already, so why do we need to typecast the findViewById method back to a Button when it is initialized as one?
Mainly, it's because
findViewById has a declared return type of
android.view.View. The Java type system isn't smart enough to read through your code and figure out that it's certainly a Button. The type system necessarily forgets a lot of type information so that it can generate generic code to handle anything that's a View, not just Buttons.