Java Runtime Polymorphism
- We will create two classes "Tree and Size".
- Size class extends Tree class and overrides its reshape() method.
- We call the reshape method by the reference variable of Parent class.
- Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is called at runtime.
Super And Sub Classes
class Tree {
public void reshape() {
System.out.println("Super tree size is big..");
}
}
class Size extends Tree {
public void reshape() {
System.out.println("Sub tree size is small..");
}
public void shape() {
super.reshape();
}
}
Main Method
public static void main(String[] args) {
// upcasting
Tree tree = new Size();
tree.reshape();
}
Comments
Post a Comment