DEV Community

Murali Rajendran
Murali Rajendran

Posted on

πŸ”„ Polymorphism in Java

1.Its that allows objects to behave differently based on their specific class type.

2.In Java, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

Features of polymorphism:
1.Multiple Behaviors: The same method can behave differently depending on the object that calls this method.
2.Method Overriding: A child class can redefine a method of its parent class.
3.Method Overloading: We can define multiple methods with the same name but different parameters.
4.Runtime Decision: At runtime, Java determines which method to call depending on the object's actual class.

Consider a person who plays different roles in life, like a father, a husband, and an employee. Each of these roles defines different behaviors of the person depending on the object calling it.

Example

// Base class Person
class Person {

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

}

// 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.");
}
Enter fullscreen mode Exit fullscreen mode

}

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

}

**
Types of Polymorphism in Java**
In Java Polymorphism is mainly divided into two types:

1.Compile-Time Polymorphism (Static)-Method overloading
2.Runtime Polymorphism (Dynamic)-Method overriding

1. Compile-Time Polymorphism
1.Compile-Time Polymorphism in Java is also known as static polymorphism and also known as method overloading. This happens when multiple methods in the same class have the same name but different parameters.
2.Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.
3.Occurs when multiple methods have the same name but different parameters (number, type, or order).
Decided at compile time.

Example:

// Method overloading By using
// Different Types of Arguments

// Class 1
// Helper class
class Helper {

// Method with 2 integer parameters
static int Multiply(int a, int b)
{
    // Returns product of integer numbers
    return a * b;
}

// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
{
    // Returns product of double numbers
    return a * b;
}
Enter fullscreen mode Exit fullscreen mode

}

// Class 2
// Main class
class Geeks
{
// Main driver method
public static void main(String[] args) {

    // Calling method by passing
    // input as in arguments
    System.out.println(Helper.Multiply(2, 4));
    System.out.println(Helper.Multiply(5.5, 6.3));
}
Enter fullscreen mode Exit fullscreen mode

}

2. Runtime Polymorphism

1.Method overriding in Java means when a subclass provides a specific implementation of a method that is already defined in its superclass.
2.Occurs when a subclass provides its own version of a method already defined in its superclass.
Decided at runtime using method overriding and dynamic method dispatch.

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
Animal obj = new Dog(); // reference of parent, object of child
obj.sound(); // Output: Dog barks
}
}

βœ… Why Use Polymorphism?

  • Increases code reusability
  • Makes code flexible and maintainable
  • Enables dynamic behavior of objects at runtime

Example

class Payment {
void pay() { System.out.println("General payment"); }
}

class CreditCard extends Payment {
void pay() { System.out.println("Paid using Credit Card"); }
}

class PayPal extends Payment {
void pay() { System.out.println("Paid using PayPal"); }
}

class Test {
public static void main(String[] args) {
Payment p = new PayPal();
p.pay(); // Output: Paid using PayPal
}
}

➑️ Same method pay() acts differently depending on the payment type.

Top comments (0)