Polymorphism in Java

Polymorphism in Java

Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, refers to the ability of an object to assume many forms.The concept of polymorphism is often expressed as “an interface, several methods”. When a general class can have the generic method and the derived classes (classes that extend the general class) they can add the class-specific implementation to that method. At the time of execution “An interface”, that is, the general class will have “many forms”, that is, references of the derived classes (see the example for clarity).

Note that the polymorphism is widely used with inheritance, where the methods of the main class are replaced by the daughter classes.

Polymorphism in Java

Two types of polymorphism in java –

  • Compilation time polymorphism (or static polymorphism)
  • Run-time polymorphism (or dynamic polymorphism)

We can make polymorphism in java by

  • Method overload (compile-time polymorphism) – In the method overload it is known at compile time itself what overloaded method will be called.
  • Replace the method (runtime polymorphism) – In the substitution of the method, it is solved in the runtime, which will be called the substituted method. The decision is based on the reference of the object for which the reference variable is pointing.

Static Polymorphism:

In Java, the static polymorphism is obtained through the overload of the method. The method overload means that there are several methods present in a class with the same name, but different types / order / number of parameters.

At compile time, Java knows which method to invoke, checking the signatures of the method. So, this is called compile time polymorphism or static binding.The following example:

class DemoOverload{

public int add(int x, int y){  //method 1

return x+y;

}

public int add(int x, int y, int z){ //method 2

return x+y+z;

}

public int add(double x, int y){ //method 3

return (int)x+y;

}

public int add(int x, double y){ //method 4

return x+(int)y;

}

}

class Test{

public static void main(String[] args){

DemoOverload demo=new DemoOverload();

System.out.println(demo.add(2,3));      //method 1 called

System.out.println(demo.add(2,3,4));    //method 2 called

System.out.println(demo.add(2,3.4));    //method 4 called

System.out.println(demo.add(2.5,3));    //method 3 called

}

}

In the previous example, there are four versions of add methods. The first method takes two parameters while the second takes three. For the third and fourth methods, there is a change in the order of the parameters. The compiler examines the signature of the method and decides which method to invoke for a specific method call at compile time.

Dynamic Polymorphism:

Suppose that a subclass replaces a specific method of the superclass. Let’s say that, in the program, we create an object of the subclass and attribute it to the reference of the superclass. Now, if we call the replaced method in the superclass reference, the subclass version of the method will be called.

Have a look at the following example.

class Vehicle{

public void move(){

System.out.println(“Vehicles can move!!”);

}

}

class MotorBike extends Vehicle{

public void move(){

System.out.println(“MotorBike can move and accelerate too!!”);

}

}

class Test{

public static void main(String[] args){

Vehicle vh=new MotorBike();

vh.move();    // prints MotorBike can move and accelerate too!!

vh=new Vehicle();

vh.move();    // prints Vehicles can move!!

}

}

It should be noted that in the first call to move (), the reference type is Vehicle and the object that is referenced is MotorBike. Therefore, when a move () call is made, Java waits until the execution time to determine which object is actually pointed to by the reference. In this case, the object is of the MotorBike class. Then, the move () method of the MotorBike class will be called. In the second call to move (), the object is of the Vehicle class. Then, the move () method of the vehicle will be called.As the method to call is determined at runtime, this is called dynamic binding or late binding.

Interested in learning Java? Enroll now:” Java training in Chennai “

Points to be observed:

  • Polymorphism, a Greek word, where poly means a lot and metamorphosis means change, therefore, refers to the ability of an object to assume many forms.
  • Any number of classes can implement an interface and each class is free to provide its own implementation. Thus, using interfaces, Java fully uses the polymorphism aspect “an interface, several methods”.
  • There are two types of polymorphism in Java, polymorphism at compile time and polymorphism at runtime.
  • Method overload is an example of compile-time polymorphism in Java.
  • The overriding method is an example of runtime polymorphism in Java.

 

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training

 

Leave a Comment