If you assign parent type to a subclass it means that you agree with to use the common features of the parent class.
It gives you the freedom to abstract from different subclass implementations. As a result limits you with the parent features.
However, this type of assignment is called upcasting.
Parent parent = new Child();
The opposite is downcasting.
Child child = (Child)parent;
So, if you create instance of Child
and downcast it to Parent
you can use that type attribute name
. If you create instance of Parent
you can do the same as with previous case but you can't use salary
because there's not such attribute in the Parent
. Return to the previous case that can use salary
but only if downcasting to Child
.