Https://www.ibm.com/developerworks/cn/java/java-language-polymorphism/index.html
Defining polymorphism
Polymorphism is a concept in object-oriented programming, meaning "different patterns", which actually means that the same method names behave differently depending on the object that implements the method.
In the context of the Java programming language, polymorphism means (through a method) that the same behavior contract is implemented in different ways for different objects. In Java, there are typically 3 ways to accomplish this:
- Interface
- Abstract methods
- Method overrides
Polymorphism via interfaces
Essentially, the interface requires that each top-level object implementation be different (the top-level object refers to the first object in the inheritance graph, which explicitly declares that it implements the interface it refers to). Polymorphism is achieved when you pass object B that implements interface a by referencing interface a instead of referencing object B.
Using abstract methods to achieve polymorphism
The Java language can also create abstract methods that can exist only on abstract classes (that is, classes that cannot be instantiated). The idea of this approach is that you must implement the abstract method (defined on abstract class A) in a hierarchy somewhere in sub-class B (assumed to be foo ()) before you can instantiate B. Polymorphism is achieved when an object of type B is passed by referencing interface A.
Implementation of polymorphism by method rewriting
When subclass B provides an alternative implementation of a method in parent Class A (assuming Foo ()), it can be said that Foo () is rewritten. When an object of type B is passed as type A to a method, the Java runtime determines whether B.foo () must actually be called, rather than A.foo (). This is how polymorphism is achieved through method overrides.
Java language polymorphism