I’ve been looking at the Android development tutorials and after looking through some code, I’ve noticed the following:
HeadlinesFragment mHeadlinesFragment;
mHeadlinesFragment = (HeadlinesFragment) getSupportFragmentManager().findFragmentById(
R.id.headlines);
I am fairly new to Java so that may be the issue, but why is this explicit casting of (HeadlinesFragment) necessary? Is mHeadlinesFragment not already of type HeadlinesFragment?
Thanks!
this returns a Fragment Object, and HeadlinesFragment is sub class, of Fragment class, and everytime you assign an object of superclass to a reference of subclass you need to cast explicitly.
You dont need explicit casting when you are assigning a subclass object to super class reference, like assigning: