DEV Community

Bharath kumar
Bharath kumar

Posted on

Polymorphism And it's types

What is Polymorphism?
• The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity can take many forms.
• Polymorphism is the ability of an object to take on many forms. Polymorphism is an important feature of Java OOPs concept and it allows us to perform multiple operations by using the single name of any method (interface).

Real Time Example of Polymorphism:
A person acts as an employee at work, a father at home, and a customer in shopping malls.

Types of Java Polymorphism:

Divided into two types:

•Compile-time Polymorphism:
Compile-Time Polymorphism in Java
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or "Method overloading"...

•Run time polymorphism:
It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by "Method Overriding".

1.Method Overloading:
• Method overloading in Java refers to the ability to define multiple methods in the same class with the same name but with different parameters.
• Java compiler differentiates these methods based on the number of parameters or the data types of parameters.

public class Calculator {

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

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

    // 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) {
        Calculator calculator = new Calculator();

        // Calling the overloaded methods
        System.out.println("Sum of 5 and 10 is: " + calculator.add(5, 10));
        System.out.println("Sum of 5, 10, and 15 is: " + calculator.add(5, 10, 15));
        System.out.println("Sum of 3.5 and 2.7 is: " + calculator.add(3.5, 2.7));
        System.out.println("Concatenation of 'Hello' and 'World' is: " + calculator.add("Hello", "World"));
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Sum of 5 and 10 is: 15
Sum of 5, 10, and 15 is: 30
Sum of 3.5 and 2.7 is: 6.2
Concatenation of 'Hello' and 'World' is: HelloWorld
Enter fullscreen mode Exit fullscreen mode

2.Method Overriding:
• A child class can redefine a method of its parent class.
Method Overriding:
• Method overriding in Java occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
• The method in the subclass should have the same name, return type, and parameter list as the method in the superclass.
• Method overriding is used to provide specific behavior for a method in a subclass.

// Base class Person
class Person {

    // Method that displays the 
    // role of a person
    void role() {
        System.out.println("I am a person.");
    }
}

// Derived class Father that 
// overrides the role method
class Father extends Person {

    // Overridden method to show 
    // the role of a father
    @Override
    void role() {
        System.out.println("I am a father.");
    }
}

public class Main {
    public static void main(String[] args) {

        // Creating a reference of type Person 
        // but initializing it with Father class object
        Person p = new Father();

        // Calling the role method. It calls the 
        // overridden version in Father class
        p.role();  
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

I am a father.
Enter fullscreen mode Exit fullscreen mode

Reference:https://logicmojo.com/polymorphism-in-oops
Reference:https://www.geeksforgeeks.org/java/polymorphism-in-java/
Reference:https://www.geekster.in/articles/polymorphism-in-java/

Top comments (0)