public interface Vegetarian {}
public class Animal {}
public class Deer extends Animal implements Vegetarian {}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:
When we apply the reference variable facts to a Deer object reference, the following declarations are legal:
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d,a,v,o refer to the same Deer object in the heap.
Polymorphism is only for instance method not for instance variable.
Java has excellent support of polymorphism in terms of Inheritance, method overloading and method overriding.
In case of overloading method signature changes while in case of overriding method signature remains same and binding and invocation of method is decided on runtime based on actual object. This facility allows Java programmer to write very flexibly and maintainable code using interfaces without worrying about concrete implementation.
It is possible to override a method by changing its return type. (if they return a subtype)
Leave a Comment