In java language, polymorphism is essentially considered into two versions.
- Compile time polymorphism (static binding or method overloading)
- Runtime polymorphism (dynamic binding or method overriding)
Here, it is important to understand the these divisions are specific to java. In context to software engineering, there are other form of polymorphisms also applicable to different languages, but for java, these are mainly considered.
Compile time polymorphism (static binding or method overloading)
As the meaning is implicit, this is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading.
In method overloading, an object can have two or more methods with same name, BUT, with their method parameters different. These parameters may be different on two bases:
1) Parameter type: Type of method parameters can be different. e.g. java.util.Math.max() function comes with following versions:
public static double Math.max(double a, double b){..}public static float Math.max(float a, float b){..}static long Math.max(long a, long b){..}public static int Math.max(int a, int b){..}
The actual method to be called is decided on compile time based on parameters passed to function in program.
2) Parameter count: Functions accepting different number of parameters. e.g. in employee management application, a factory can have these methods:
EmployeeFactory.create(String firstName, String lastName){...}EmployeeFactory.create(Integer id, String firstName, String lastName){...}
Both methods have same name “create” but actual method invoked will be based on parameters passed in program.
Runtime polymorphism (dynamic binding or method overriding)
Runtime polymorphism is essentially referred as method overriding. Method overriding is a feature which you get when you implement inheritance in your program.
A simple example can be from real world e.g. Animal. An application can have Animal class, and its specialized sub classes like Cat and Dog. These subclasses will override the default behavior provided by Animal class + some of its own specific behavior.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public class Animal { public void makeNoise() { System.out.println("Some sound"); }}class Dog extends Animal{ public void makeNoise() { System.out.println("Bark"); }}class Cat extends Animal{ public void makeNoise() { System.out.println("Meawoo"); }} |
Now which makeNoise() method will be called, depends on type of actual instance created on runtime e.g.
1
2
3
4
5
6
7
8
9
10
| public class Demo{ public static void main(String[] args) { Animal a1 = new Cat(); a1.makeNoise(); //Prints Meowoo Animal a2 = new Dog(); a2.makeNoise(); //Prints Bark }} |
Important points:
- Polymorphism is the ability to create a variable, a function, or an object that has more than one form.
- In java, polymorphism is divided into two parts : method overloading and method overriding.
- Some may argue that method overloading is not polymorphism. Then what does the term compile time “polymorphism” means??
- Another term operator overloading is also there, e.g. “+” operator can be used to add two integers as well as concat two sub-strings. Well, this is the only available support for operator overloading in java, and you can not have your own custom defined operator overloading in java.
No comments:
Post a Comment