What is Polymorphism in Java?

0

Polymorphism

Polymorphism is a core concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It provides flexibility and extensibility in programming, enabling code to be written that can work with different types of objects without the need for explicit type checking.


Polymorphism 

Types of Polymorphism in Java

There are two types of polymorphism in Java:

  1. Compile-time Polymorphism (Static Binding): Also known as method overloading, compile-time polymorphism is achieved by having multiple methods with the same name but different parameter lists. The appropriate method to invoke is determined at compile-time based on the arguments passed to it.

  2. Runtime Polymorphism (Dynamic Binding): Also known as method overriding, runtime polymorphism is achieved by having a method in the subclass with the same name and signature as a method in its superclass. The appropriate method to invoke is determined at runtime based on the actual type of the object.

Examples of Polymorphism in Java

  1. Compile-time Polymorphism (Method Overloading):

Consider the following class with two overloaded methods:

public class Calculator
 {   
 public int add(int a, int b)
 {        
  return a + b;
 }        
 public double add(double a, double b)
 {   
     return a + b;   
 }
 }

In the above example, the add method is overloaded with two versions: one that takes two integers and returns an integer result, and another that takes two doubles and returns a double result. Depending on the arguments passed to the add method, the appropriate version will be invoked at compile-time.

  1. Runtime Polymorphism (Method Overriding):

Consider the following hierarchy of classes:

public class Animal
{    
  public void makeSound()
{       
   System.out.println("Animal makes a sound");
}
}
  public class Dog extends Animal
{   
   @Override    
   public void makeSound()
{        
    System.out.println("Dog barks");
}
}
public class Cat extends Animal
{   
 @Override 
   public void makeSound()
{        
System.out.println("Cat meows"); 
}
}

In the above example, the Animal class is the superclass of the Dog and Cat classes. Each subclass overrides the makeSound method defined in the Animal class. At runtime, the appropriate version of the makeSound method is determined based on the actual type of the object.

Animal animal1 = new Dog();Animal 
animal2 = new Cat();animal1.makeSound(); 
// Output: "Dog barks"animal2.makeSound(); 
// Output: "Cat meows"

In the above example, even though the variables animal1 and animal2 are declared as type Animal, they can hold objects of different subclasses. The method makeSound is invoked on each object based on its actual type, resulting in different outputs.

Benefits of Polymorphism

  • Code reusability: Polymorphism allows different objects to be used interchangeably, promoting code reuse and reducing redundancy.

  • Flexibility: With polymorphism, code can work with different types of objects without the need for explicit type checking, making it easier to accommodate future changes or additions to the code.

  • Extensibility: Polymorphism provides a mechanism for adding new subclasses or types without modifying the existing code, increasing the extensibility of the system.

Polymorphism is a powerful concept in Java that enables code to be more flexible, reusable, and extensible. It plays a crucial role in achieving the goals of object-oriented programming. 

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*