Java Polymorphism

Polymorphism in Java allows the same method or object to take multiple forms, enabling flexibility and reusability in code. It is mainly achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).

Types of Polymorphism in Java

The following are the types of Polymorphism in Java:

  1. Compile-Time Polymorphism (Method Overloading)
  2. Runtime Polymorphism (Method Overriding)

Let us understand them with examples:

Compile-Time Polymorphism (Method Overloading)

Method Overloading is achieved when multiple methods have the same name but different parameter lists. It is called compile-time polymorphism because the decision of which method to call is made at compile time.

Let us see an example to implement method overloading in Java:

class Calculate {
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
    int add(int a, int b, int c) {
        return a + b + c;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculate obj = new Calculate ();
        System.out.println(obj.add(7, 15));       // Calls int version
        System.out.println(obj.add(3.5, 6.5));   // Calls double version
        System.out.println(obj.add(5, 20, 30));    // Calls 3-parameter version
    }
}

Output

22
10.0
55

Runtime Polymorphism (Method Overriding)

Method overriding is achieved when a subclass provides a specific implementation of a method already defined in its parent class. It is called Runtime polymorphism because the method call is resolved at runtime based on the object type.

In the example below, we perform Method Overriding. Each subclass (Sparrow, Crow, Parrot) overrides the sound() method of the parent class Bird:

// Run-time polymorphism (Method Overriding) in Java

class Bird {
    void sound() {
        System.out.println("Birds make sounds...");
    }
}

// Child class 1
class Sparrow extends Bird {
    void sound() {
        System.out.println("Sparrow chirps...");
    }
}

// Child class 2
class Crow extends Bird {
    void sound() {
        System.out.println("Crow caws loudly..");
    }
}

// Child class 3
class Parrot extends Bird {
    void sound() {
        System.out.println("Parrot mimics..");
    }
}

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

        // A reference variables b is declared
        Bird b = new Bird();

        // Runtime Polymorphism
        /* When b.sound() is called, the version of sound() belonging to
        the actual object type is executed */

        // This calls Sparrow's version of sound()
        b = new Sparrow();
        b.sound();

        // This calls Crow's version of sound()
        b = new Crow();
        b.sound();

        // This calls Parrot's version of sound()
        b = new Parrot ();
        b.sound();
    }
}

Output

Sparrow chirps...
Crow caws loudly..
Parrot mimics..

Method Overriding vs Method Overloading

Let us compare Compile-Time and Run-Time polymorphism:

Compile Time vs Run Time Polymorphism in Java


If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Scope of Variables in Java
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment