The Power of Polymorphism in Java: How to Use Method Overloading and Overriding


 

What is Polymorphism in Java?

Polymorphism is a core concept in object-oriented programming that refers to the ability of a single entity (such as a method or object) to take on multiple forms. In other words, polymorphism allows you to write code that can work with multiple data types and objects, even if they are not of the same type.

Types of Polymorphism in Java

There are two main types of polymorphism in Java: static polymorphism and dynamic polymorphism.

Static Polymorphism

Static polymorphism, also known as method overloading, occurs when a class has multiple methods with the same name but different parameters. This allows you to create multiple versions of a method that can be called depending on the number or type of arguments passed to the method.

For example, consider the following class that has three versions of the add method:

public class Calc{
 
  public static double add(double x, double y) {
    return x + y;
  }
  public static int add(int x, int y) {
    return x + y;
  }
  public static int add(int x, int y, int z) {
    return x + y + z;
  }

}

In this case, you can call the add method with either two int arguments, two double arguments, or three int arguments, and the appropriate version of the method will be called based on the arguments passed.

Dynamic Polymorphism

Dynamic polymorphism, also known as method overriding, occurs when a subclass has a method with the same name and signature as a method in the superclass. This allows the subclass to "override" the behavior of the superclass method and provide its own implementation.

For example, consider the following class hierarchy:

public class Animal{
  public void makeSound() {
    System.out.println("Some generic animal sound");
  }
}

public class Dog extends Animal {
  @Override
  public void makeSound() {
    System.out.println("Woof, Woof!");
  }
}

In this case, the Dog class has overridden the makeSound method from the Animal class and provided its own implementation. If you have a reference to a Dog object and call the makeSound method, it will print "Woof!", rather than the generic animal sound from the Animal class.

Example of Using Polymorphism in Java

Here is an example of how you can use polymorphism in a Java program:

public class Main {
  public static void main(String[] args) {
    // Static polymorphism example
    int result = Calc.add(1, 2); // calls add(int, int)
    double result2 = Calc.add(1.0, 2.0); // calls add(double, double)
    
    // Dynamic polymorphism example
    Animal a = new Animal();
    a.makeSound(); // prints "Some generic animal sound"
    
    Animal b = new Dog();
    b.makeSound(); // prints "Woof, Woof!"
  }
}

In this example, the Calc class is used to demonstrate static polymorphism, and the Animal and Dog classes are used to demonstrate dynamic polymorphism. The first call to the add method in the Calc class uses two int arguments, so it calls the add(int, int) method. The second call uses two double arguments, so it calls the add(double, double) method.

In the second part of the example, we create an Animal object and a Dog object and call the makeSound method on each of them. The Animal object calls the makeSound method from the Animal class, which prints a generic animal sound. The Dog object, on the other hand, calls the overridden version of the makeSound method from the Dog class, which prints "Woof!".

Benefits of Polymorphism

Polymorphism is a powerful tool in object-oriented programming, as it allows you to write code that is flexible and reusable. By using polymorphism, you can create classes that can be extended and customized by subclasses, without having to change the original code. This can make your code easier to maintain and debug, as you only have to modify a single version of a method rather than multiple copies.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !