Polymorphism is derived from the Greek word poly – meaning many, and morphe – meaning form.
In OOP, there are 4 major kinds of polymorphism:
1.Static polymorphism (classes)
2. Dynamic polymorphism (inheritance)
3. Inclusion polymorphism (inheritance)
4. Parametric polymorphism* (generics)
Static polymorphism is a polymorphism evident during earlybinding.
Early binding means during compile time. In Java, static polymorphism is also called methodoverloading.
Method overloading is implementing multiple methods of the same class that uses the samename but with different sets of parameters.
Static Polymorphism
The parameter sets have to differ in at least one of the following criteria:
1.Number of parameters
2. Data types of parameters
3. Order of parameters
Since the sets of parameters for each method overload is different, each method has a different signature.
Dynamic polymorphism is a polymorphism evident during late binding.
Late binding means this form of polymorphism doesn’t allow the compiler to determine the executed method. The method executed is only chosen during runtime.
Late binding means this form of polymorphism doesn’t allow the compiler to determine the executed method. The method executed is only chosen during runtime.
In Java, dynamic polymorphism is represented in the operation of methodoverriding which is a benefit of inheritance.
Inclusion polymorphism is another polymorphism evident during late binding. Inclusion polymorphism is also known as subtyping.
Subtyping, another benefit of inheritance, is the ability of any ancestor class identifier to reference a descendant class object.
If a subtype object is represented by an ancestor type identifier, you can only call methods existing on the ancestor type identifier.
If a method of that ancestor type is overridden along the subclasses leading to the subtype, then the nearest class of the subtype along that ancestry that overrides that method is the one that will be executed when that specific method is called.
To know whether an object is inclusion polymorphic, you can perform an instanceof test.