java

Java Polymorphism


What is Polymorphism?

Polymorphism is a fundamental concept that is derived from two words, poly and morphism which meaning is manyforms. It allows single object to behave differently according to the situation, so we can say if the same object having different behaviour that is called polymorphism.

Syntax

public class Calculator {
    public int add(int a, int b) {
       // code;
    }

    public double add(double a, double b) {
       // code;
    }
}

Note :- You can see in the above syntax there are two methods with the same name but their behavior is different if we look at the parameters.

 

Types of Polymorphism

 

1. Compile-time Polymorphism:

  • Method Overloading:
    Method overloading allows a class to have multiple methods with the same name but different parameter lists.

Syntax

public class Calculator {
    public int show(string name) {
       // code;
    }

    public double add(int num) {
        // code;
    }
}

Example

public class Addition {

    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Method to add two doubles
    public double add(double a, double b) {
        return a + b;
    }

    // Method to concatenate two strings
    public String add(String a, String b) {
        return a + b;
    }

    public static void main(String[] args) {
        Addition obj = new Addition();
        

		int sumInt = obj.add(5, 7);
        System.out.println("Sum of integers: " + sumInt);


        double sumDouble = obj.add(3.5, 2.8);
        System.out.println("Sum of doubles: " + sumDouble);


        String concatStrings = obj.add("Hello, ", "world!");
        System.out.println("Concatenated strings: " + concatStrings);
    }
}

 

  • Constructor Overloading:
    Constructor overloading enables a class to have multiple constructors with different parameter lists.

Example

public class Add{

    private int result;

    // Constructor to add two integers
    public Add(int a, int b) {
        result = a + b;
    }

    // Constructor to add two doubles
    public Add(double a, double b) {
        result = (int) (a + b); // Assuming we want the result as an integer
    }

    public void disp() {
        System.out.println("Result: " + result);
    }

    public static void main(String[] args) {
        // Using the constructor to add two integers
        Adder adderInt = new Add(5, 7);
        adderInt.disp();

        // Using the constructor to add two doubles
        Adder adderDouble = new Add(3.5, 2.8);
        adderDouble.disp();
    }
}

 

2. Runtime Polymorphism:

  • Method Overriding: ( using class )
    Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

Syntax

class Shape {
    void Draw() {
       // code;
    }
}

class Circle extends Shape {
    @Override
    void Draw() {
       // code;
    }
}

Example

class Shape {
    public void draw() {
       System.out.println("Can't Say about Shape type");
    }
}

class Circle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing Circle shape");
    }
}

class Rectangle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing Rectangle shape");
    }
}

public class DrawingApp {
    public static void main(String[] args) {
        // Creating instances of Circle and Rectangle
        Shape circle = new Circle();
        circle.draw();
        Shape rectangle = new Rectangle();
        rectangle.draw();
    }

}

 

  • Method Overriding: ( using interface )
    Interfaces allow us to achieve polymorphism by allowing objects of different classes to be treated as objects of the same interface type.

Example

interface Drawable {
    void draw();
}

class Shape implements Drawable {
    @Override
    public void draw() {
        System.out.println("Can't Say about Shape type");
    }
}

class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing Circle shape");
    }
}

class Rectangle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing Rectangle shape");
    }
}

public class DrawingApp {
    public static void main(String[] args) {
        // Creating instances of Circle and Rectangle
        Drawable circle = new Circle();
        circle.draw();
        
        Drawable rectangle = new Rectangle();
        rectangle.draw();
    }
}