Java Polymorphism (Part 4)

 

Java Polymorphism 

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.


Why to use polymorphism ?

              ▪ It helps the programmer to reuse the codes, i.e., classes once written, tested and                     implemented can be reused as required. Saves a lot of time
  •   Single variable can be used to store multiple data types.

  •   Easy to debug the codes


There are 2 types of polymorphism :


1. Compile-time polymorphism

    Whenever an object is bound with their functionality at the compile-time, this is known as the compile-time polymorphism. Compile-time polymorphism is achieved through method overloading

Method Overloading says you can have more than one function with the same name in one class having a different parameter.

eg:- Here I am declaring add( ) which adds two values and overloading it with different parameters.

 class Main {  
      public void add(int num) {  
           System.out.println("Addition of " + num + " and 0 is : " + num);  
      }  
      public void add(int num, int num1) {  
           System.out.println("Addition of " + num + " and " + num1 + " is : "+ (num + num1));  
      }  
      public void add(double num, double num1) {  
           System.out.println("Addition of " + num + " and " + num1 + " is : "+ (num + num1));  
      }  
 }  
 public class MyClass {  
      public static void main(String[] args) {  
           Main obj = new Main();  
           obj.add(5);  
           obj.add(5, 10);  
           obj.add(5.5, 10.5);  
      }  
 }  

Output:-

 Addition of 5 and 0 is : 5  
 Addition of 5 and 10 is : 15  
 Addition of 5.5 and 10.5 is : 16.0  

This is an example of compile-time polymorphism as when we pass arguments into methods it is decided at compile-time that which method should be called.


2. Run-time polymorphism

     Whenever an object is bound with their functionality at the run-time, this is known as the run-time polymorphism. Run-time polymorphism is achieved through method overriding

If child class has the same method as declared in the parent class, it is known as method overriding in Java.

eg:- Here I am declaring a Parent Class and inheriting it with three other classes , a method rateOfInterest( ) is declared in Parent Class and is overridden in its child classes.

 //creating a parent class  
 class Bank {  
      public double rateOfInterest() {  
           return 0;  
      }  
 }  
 // creating child classes  
 class SBI extends Bank {  
      public double rateOfInterest() {  
           return 7.8;  
      }  
 }  
 class AXIS extends Bank {  
      public double rateOfInterest() {  
           return 8.2;  
      }  
 }  
 class HDFC extends Bank {  
      public double rateOfInterest() {  
           return 8.6;  
      }  
 }  
 public class MyClass {  
      public static void main(String[] args) {  
           SBI s = new SBI();  
           AXIS a = new AXIS();  
           HDFC h = new HDFC();  
           System.out.println("Rate of Interest of SBI: " + s.rateOfInterest());  
           System.out.println("Rate of Interest of AXIS: " + a.rateOfInterest());  
           System.out.println("Rate of Interest of HDFC: " + h.rateOfInterest());  
      }  
 }  
Output:-
 Rate of Interest of SBI: 7.8  
 Rate of Interest of AXIS: 8.2  
 Rate of Interest of HDFC: 8.6  
 This is an example of run-time polymorphism as Java virtual machine determines the proper method to call at the runtime, not at the compile time. 

Difference between Compile-time Polymorphism and Run-time Polymorphism 

Compile-time Polymorphism Run-time Polymorphism
In Compile time Polymorphism, the call is resolved by the compiler. In Run time Polymorphism, the call is not resolved by the compiler.
It is also known as Static binding, Early binding and overloading as well. It is also known as Dynamic binding, Late binding and overriding as well.
Method overloading is the compile-time polymorphism where more than one methods share the same name with different parameters or signature and different return type. Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes.
It provides fast execution because the method that needs to be executed is known early at the compile time. It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime.
Compile time polymorphism is less flexible as all things
execute at compile time.
Run time polymorphism is more flexible as all things execute at run time.



< Previous Next >