Polymorphism in Java

 

Polymorphism in Java

 

Polymorphism in Java is one of the core Object-Oriented Programming (OOP) principles, along with classes and objects, encapsulation, abstraction, and inheritance. The word “polymorphism” comes from Greek, meaning "many forms". In Java, polymorphism allows an object to take many forms by performing a single action in different ways, depending on the context – the same method name or operator can perform different tasks, depending on:

  • the object that invokes it, or
  • the parameters passed to it.

 

Types of Polymorphism in Java

Java supports two main types of polymorphism:

 

1. Compile-Time Polymorphism (Static Binding / Method Overloading)

  • Achieved using method overloading or operator overloading (though Java does not support custom operator overloading).
  • Decision about which method to call is made at compile time.
  • Same method name but different parameter lists (different number or type of arguments).

 

2. Run-Time Polymorphism (Dynamic Binding / Method Overriding)

  • Achieved using method overriding (a subclass provides a specific implementation of a method already defined in its parent class).
  • Decision about which method to call is made at runtime, based on the actual object type, not the reference type.
  • Supports dynamic method dispatch – the process of resolving which overridden method to call at runtime, rather than at compile time. It is the mechanism that enables run-time polymorphism in Java.

 

 

// Compile time Polymorphism

// Method overloading

 

class Area

{

         public void fnArea(int s)

         {

                 System.out.println("Area of square = "+(s*s));

         }

         public void fnArea(double r)

         {

                 System.out.println("Area of circle = "+(3.142*r*r));

         }

         public void fnArea(int l, int b)

         {

                 System.out.println("Area of Rectangle = "+(l*b));

         }

}

 

class MO

{

         public static void main(String as[])

         {

                 Area obj = new Area();

                 int x = 5, y = 7;

                 double r = 7.0;

                

                 obj.fnArea(x);

                 obj.fnArea(x,y);

                 obj.fnArea(r);

         }

}

 

Sample output

> javac MO.java

> java MO

Area of square = 25

Area of Rectangle = 35

Area of circle = 153.958

> 

 

Method overloading – Compile time polymorphism

The Three methods of class Area

1.   Have the same method name – fnArea

But differ by

2.   Number of arguments and

3.   Datatype of arguments

 

 

// Run time Polymorphism

// Method Overriding

// Dynamic method dispatch

 

class A

{

         public void fnDisplay()

         {

                 System.out.println("Class A");

         }

}

 

class B extends A

{

         public void fnDisplay()

         {

                 System.out.println("Class B");

         }

}

 


 

class C extends B

{

         public void fnDisplay()

         {

                 System.out.println("Class C");

         }

}

 

class DMD

{

         public static void main(String as[])

         {

                 A obja = new A();

                 B objb = new B();

                 C objc = new C();

                

                 A refObj;

                

                 refObj = obja;

                 refObj.fnDisplay();

        

                 refObj = objb;

                 refObj.fnDisplay();

        

                 refObj = objc;

                 refObj.fnDisplay();

         }

}

 

Sample Output

> javac DMD.java

> DMD

Class A

Class B

Class C

> 

Even though refObj is declared as type A, it executes different fnDisplay() methods depending on the actual object (A, B, or C) it points to.

 

 

Key Difference

 

Feature

Compile-Time Polymorphism (Overloading)

Run-Time

Polymorphism (Overriding)

Binding time

Compile-time

(early binding)

Runtime

(late binding)

How achieved?

Method Overloading

Method Overriding

Parameters

Must differ

(number/type/order)

Must be the same

Return type

Can be different

if parameters differ

Must be same

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu