Polymorphism
Summary
reference variable is child : the number of usable members is itself. if there is n members, the number of usable members is n as well.
reference variable is parent : the number of usable members is as same as parent's one. when usable members of parent is m, and usable members of child is n, in this case the number of usable members is m of n.(m<n)

Handle child type object using parent(ancestor) type reference variable. Type mismatch is allowed like below : when the relation is parent and child.
class Tv {
boolean power;
int channel;
void power() {power = != power;}
void channelUp() {++channel;}
void channelDown() {--channel;}
}
class SmartTv extends Tv {
String text;
void caption() {..}
}
//parent type reference var <= child type reference
Tv t = new SmartTv();
//Tv t = new Tv();
//SmartTv s = new SmartTv ();
parent >= child : 5 of 7.(O)
child >= parent : 7 of 5.(X)
//Having 7 members
SmartTv s = new SmartTv();
//Can use 5 of 7 members. It is going to be pros of polymorphism!
Tv t = new SmartTv();
//vice versa, child reference type CNANNOT take parent type object!
//SmartTv s = new Tv();
Last updated
Was this helpful?