DEV Community

Ajay Sundar
Ajay Sundar

Posted on

Polymorphism in Java

What is Polymorphism ?

Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. 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.

Key Features of java

  • Multiple Behaviors: The same method can behave differently depending on the object that calls this method.

  • Method Overriding: A child class can redefine a method of its parent class.

  • Method Overloading: We can define multiple methods with the same name but different parameters.

  • Runtime Decision: At runtime, Java determines which method to call depending on the object's actual class.

Reference : https://www.geeksforgeeks.org/java/polymorphism-in-java/

Overloading in Polymorphism


public class Sum {

    public int sum(int x, int y) { 
    return (x + y); }

    public int sum(int x, int y, int z)
    {
        return (x + y + z);
    }

    public double sum(double x, double y)
    {
        return (x + y);
    }


    public static void main(String args[])
    {
        Sum s = new Sum();
        System.out.println(s.sum(10, 20));
        System.out.println(s.sum(10, 20, 30));
        System.out.println(s.sum(10.5, 20.5));
    }
}

OUTPUT

30
60
31.0

Enter fullscreen mode Exit fullscreen mode

Line by Line Explanation

  • public class Sum { ... }

Declares a public class named Sum. Because the class is public, the source file must be named Sum.java.

  • public int sum(int x, int y)

A method named sum that accepts two int parameters and returns an int. The body returns the sum x + y.

  • public int sum(int x, int y, int z)

Another sum method with three int parameters. This is overloading — same method name but different parameter list (arity).

  • public double sum(double x, double y)

Another overloaded sum that accepts two doubles and returns a double. Because parameter types differ from the int version, Java considers it a different method.

  • public static void main(String args[])
    The JVM looks for this method to start the program. static means it runs without creating an instance of Sum.

  • Sum s = new Sum();
    Creates a new Sum object and stores its reference in variable s.

  • System.out.println(s.sum(10, 20));
    Calls the sum(int, int) method. Integer literals 10 and 20 are of type int by default. The method returns 30, which println prints.

  • System.out.println(s.sum(10, 20, 30));
    Calls the sum(int, int, int) method. Returns 60.

  • System.out.println(s.sum(10.5, 20.5));
    Calls the sum(double, double) method. Decimal literals like 10.5 are double by default. Returns 31.0.

Method overriding example with a car race

class Vehicles {
    void Car(){
        System.out.println("Vehicles are faster");
    }
}
class Race extends Vehicles {
     void Car(){
        System.out.println("Porsche is ready for the car race");
    }
}
class Slowrace extends Vehicles {
     void Car(){
        System.out.println("Benz is ready for the slow race");
    }
}
public class Tokyo {
    public static void main(String[] args){
        Vehicles vehicle1 = new Vehicles();
        Race vehicle2 = new Race();
        Slowrace vehicle3 = new Slowrace();

        vehicle1.Car();
        vehicle2.Car();
        vehicle3.Car();



    }
}

OUTPUT

Vehicles are faster
Porsche is ready for the car race
Benz is ready for the slow race

Enter fullscreen mode Exit fullscreen mode

Top comments (0)