Polymorphism in Java

Java Polymorphism -

It is a process of creating a class with multiple methods with same method name with different implementations is called polymorphism.

Implementation of polymorphism -

We can implement polymorphism by using

  • Method Overriding
  • Method Overloading

To develop polymorphism we must define method in all subclass with same name with same prototype as it is declared in superclass.

Java Supports two types of polymorphism -

  1. Compile-time polymorphism
  2. Run-time polymorphism

Compile time polymorphism or Static binding or Early binding

When a method is invoked, if its method definition which is bind at compile time by compiler is only executed by JVM at runtime. Then it is called compile-time polymorpism or static binding or early binding.

Run-time Polymorphism or Dynamic binding or Late binding

When a method is invoked, the method definition which is bind at compile time does not executed at run-time, instead of that is it executed from subclass then it is called runtime polymorphism.

Only non-static overridden method comes under run-time polymorphism. private non-static methods and default non-static mehtod from outside package are not overridden. SO these method call comes under compile time polymorphism.

Definition of Method Overriding -

Redefine super class method in subclass to provide new implementation is called method overriding.

Example -

//A.java
class A {
      void add(int a,int b) {
           System.out.println(a+b);
        }
   }
//B.java
class B {
      void add(int a,int b) {
           System.out.println("Addition Result "+(a+b));
      }
  }
----------------------------------------------------------------------------
 When we call -
 A a1 = new A();
 a1.add(10,2);
 Output - 12
 B b1 = new B();
 b1.add(7,3);
 Output - Addition result 10
 A a2 = new B();  
   a2.add(2,9);

In B class we are overriding 'add' method, we are overriding B class add method because we have added new feature in B class.

In the above program when we execute add method by using B object it is always execut from B class.

For statement a2.add(22,9) compiler bind add method from A class because compiler only consider reference variable type.

JVM also first enter into A class with current object and arguments i.e B obje and 2,9.

Then from super class add method it checks is this add method overidden in current object that is B.

Since this method is overridden in B class JVM will execute add method from B class.

Conclusion is JVM will always check for current object at run-time not its reference.

Method Overloading -

More than one method with same name but different parameter or type or list or order define in the same class is called method overloading in java.

Why Overloading ? -

To execute same logic with different type of input values we must overload a method.

Example -

//Addition.java
class Addition { 
     static void add(int a, int b);
     System.out.println("Addition of integer is : "+(a+b));
     }
     static void add(float a, float b) {
     System.out.prinln("Addition of float is : "+(a+b));
   }
   public static void main(String... args) {
          add(4,7);
          add(5f,10f);
 }
}

Overloading in Sub-class

We can also overload method in sub-class as well.

Example -

//A.java
class A {
      void m1() {
           System.out.println("m1 no-argument");
      }
      void m1(int x) {
           System.out.println("m1 int argument");
      }
 }
//B.java
 class B {
       void m1(String s) {
            System.out.println("m1 string agrument");
       }
       void m1(double d) {
            System.out.println("m1 double argument");
       }
 }

Leave a Comment

%d bloggers like this: