because of polymorphic reference, when you store a sub-type
object to super-type
reference, you can't use method specific to sub-type
. but you can just get which are methods defined in super-type
.
to be able to use sub-type methods, you need to cast the reference of super-type
to sub-type
.
Parent p = new Child();
...
...
((Child)p).childMethod();
but this is only possible when you know object assigned to p
is actually of type Child
. else it will give ClassCastException