I see this quite a lot,
A method which returns an object, i.e.
public Object getGroup(int groupPosition)
{
return groups.get(groupPosition);
}
Then when this function is called, the returned object is casted to a certain class, i.e.
ExpandListGroup group = (ExpandListGroup) getGroup (groupPosition);
It seems to be the case that if a plain object is returned, you know the class of that object (TestClass) and you want to set a predeclared object (X) to that returned object (Y), you need to cast the corresponding class in the form of..
TestClass X = (TestClass) returnsY();
Is this correct? Is there any other deeper meaning / consequence of casting an object as a class?
Cheers
All references needs to hold objects of same type as reference or its subtypes. Since compiler knows only that
getGroupreturnsObjectit can’t let Object to be assigned toTestClassreference (Object may not have implemented all methods that TestClass have). To solve that problem you need to explicitly tell compiler that object returned bygetGroupis alsoTestClassclass by casting it.