Polymorphism in Java

polymorphism in java

What is Polymorphism

The word Polymorphism, in general, is made up of two words poly: meaning many and morph: meaning form. 

 

Understanding Polymorphism

Polymorphism plays a vital role in allowing objects that have a different internal structure to share the same external interface. Polymorphism is extensively used in implementing inheritance.

Polymorphism is of two types:

  • Compile-time Polymorphism (static or early binding)
  • Run-time Polymorphism (dynamic or late binding)

Let us understand polymorphism with the help of an example.

Consider we have a superclass Shape that has a method shapeArea(). Subclasses of shape can be Triangle, Rectangle,Circle, etc.

Run
class ShapeArea
{  
    void area()
    {
        System.out.println("Area");
        
    }  
}  
class Circle extends ShapeArea
{  
void area()
    {
        System.out.println("Area of Circle" );
    
    }  
}  
class Rectangle extends ShapeArea
{  
    void area()
    {
        System.out.println("Area of Rectangle " );
        
    }  
}  
class Triangle extends ShapeArea
{  
    void area()
    {
        System.out.println("Area of Triangle " );
        
    }  
}  
public class Main
{  
    public static void main(String args[])
    {  
        ShapeArea s;  
        s=new Circle();  
        s.area();  
        s=new Rectangle();  
        s.area();  
        s=new Triangle();  
        s.area();  
    }  
}  
java polymorphism

Compile-time Polymorphism

Also known as Static polymorphism, is the type of polymorphism that is resolved at the time of compilation. It can be achieved by either method overloading or operator overloading.

  • When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by a change in the number of arguments or/and change in the type of arguments.
  • For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.

Example

Run
class ComplitetimePolymorph { 
  
    void operator(String str1, String str2) 
    { 
        String s = str1 + str2; 
        System.out.println("Concatenated String - " + s); 
    } 
  
    void operator(int a, int b) 
    { 
        int c = a + b; 
        System.out.println("Sum = " + c); 
    } 
} 
  
class Main { 
    public static void main(String[] args) 
    { 
        ComplitetimePolymorph obj = new ComplitetimePolymorph(); 
        obj.operator(10, 20); 
        obj.operator("Hey", "Prepster"); 
    } 
} 

Output

Sum = 30

Concatenated String- HeyPrepster

Runtime Polymorphism

Runtime or dynamic Polymorphism is the polymorphism which resolves dynamically at the runtime rather than compile-time is called. We can also call it as dynamic binding or Dynamic Method Dispatch. Since the method invocation is during runtime and not during compile-time, this type of polymorphism is called Runtime or dynamic polymorphism.

We can achieve dynamic polymorphism in Java with the help of Method Overriding.

In an object-oriented language, Method overriding occurs when a derived class provides a specific definition of the method that is already present in its parent class. We say that the function in the base class is being overridden.

Run
class Animal { 
  
    void Print() 
    { 
        System.out.println("parent class"); 
    } 
} 
  
class Dog extends Animal { 
  
    void Print() 
    { 
        System.out.println("Dog barks"); 
    } 
} 
  
class Cat extends Animal { 
  
    void Print() 
    { 
        System.out.println("Cat meows"); 
    } 
} 
  
class Main { 
    public static void main(String[] args) 
    { 
  
        Animal a; 
  
        a = new Dog(); 
        a.Print(); 
  
        a = new Cat(); 
        a.Print(); 
    } 
} 

Output

Dog barks

Cat meows

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription