DEV Community

KIRAN RAJ
KIRAN RAJ

Posted on

Polymorphism in java

what is polymorphism in java?

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. In Java, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

Key features of polymorphism:

Multiple Behaviors: The same method can behave differently depending on the object that calls this method.(TBD)
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.(TBD)

What is method overloading in java?

In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference may include:

The number of parameters
The types of parameters
The order of parameters

Key features of Method Overloading

  • Multiple methods can share the same name in a class when their parameter lists are different.

  • Overloading is a way to increase flexibility and improve the readability of code.

  • Overloading does not depend on the return type of the method, two methods cannot be overloaded by just changing the return type.


public class Sum {

    // Overloaded sum() 
    // This sum takes two int parameters
    public int sum(int x, int y) { return (x + y); }

    // Overloaded sum()
    // This sum takes three int parameters
    public int sum(int x, int y, int z)
    {
        return (x + y + z);
    }

    // Overloaded sum() 
    // This sum takes two double parameters
    public double sum(double x, double y)
    {
        return (x + y);
    }

    // Driver code
    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));
    }
}
Enter fullscreen mode Exit fullscreen mode
30
60
31.0
Enter fullscreen mode Exit fullscreen mode

What is method overriding in java?

Overriding in Java occurs when a subclass or child class implements a method that is already defined in the superclass or base class. When a subclass provides its own version of a method that is already defined in its superclass, we call it method overriding. The subclass method must match the parent class method's name, parameters, and return type.

Rules for Overriding:

  • Name, parameters, and return type must match the parent method.

  • Java picks which method to run, based on the actual object type, not just the variable type.

  • Static methods cannot be overridden.

  • The @override annotation catches mistakes like typos in method names.


// Example of Overriding in Java
class Animal {
    // Base class
    void move() { System.out.println(
      "Animal is moving."); }
    void eat() { System.out.println(
      "Animal is eating."); }
}

class Dog extends Animal {
    @Override void move()
    { // move method from Base class is overriden in this
      // method
        System.out.println("Dog is running.");
    }
    void bark() { System.out.println("Dog is barking."); }
}

public class Geeks {
    public static void main(String[] args)
    {
        Dog d = new Dog();
        d.move(); // Output: Dog is running.
        d.eat(); // Output: Animal is eating.
        d.bark(); // Output: Dog is barking.
    }
}
Enter fullscreen mode Exit fullscreen mode
Dog is running.
Animal is eating.
Dog is barking.
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)