I have two classes:
class ItemInfo {
public View createItemView() {
View v;
// ...
v.setTag(this);
return v;
}
}
class FolderInfo extends ItemInfo {
@Override
public View createItemView() {
View v;
// ...
v.setTag(this);
return v;
}
}
Then I use it:
FolderInfo folderInfo;
// Create it here
ItemInfo itemInfo = folderInfo;
View v = itemInfo.createItemView();
Object objectTag = v.getTag();
Then I check type of objectTag by instanceof, and it’s ItemInfo! Why?
If you do this:
You’ll ofcourse see
"OK!"being printed, becauseFolderInfois a subclass ofItemInfo– so aFolderInfois also anItemInfoobject.Inheritance means that there is an “is a” relationship from the subclass to the superclass – see Liskov substitution principle.