Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialArthur Khakimov
6,195 PointsWhy cast to MockedView?
We instantiate the view as "view = new MockedView()". The MockedView class defines the "textViewText" variable.
However, in assertion, we must cast our "view" variable to its class as "(MockedView) view" to be able to access the "textViewText" variable.
Can you please explain the reason why we can't read this variable just by "view.textViewText", or why we must cast to read it?
Thanks.
2 Answers
Ben Deitch
Treehouse TeacherHey Arthur!
At the top of the class we declare 'view' as a MainActivityView. So as far as the compiler knows, 'view' is just a MainActivityView and doesn't have a 'textViewText' property. We cast it to a MockedView to let Android know that, yes it's a MainActivityView, but more specifically it's a MockedView which gives us access to the 'textViewText' property.
Arthur Khakimov
6,195 PointsUnderstood! Thank you, Ben!