Polymorphism in Java

Polymorphism in Java is a powerful feature of object-oriented programming (OOP) that allows us to perform a single action in different ways. In Java, polymorphism is achieved through two mechanisms: method overloading and method overriding.

Why Polymorphism is Important in Java?

Polymorphism is important in Java because it allows us to write more flexible and reusable code. For example, we can use polymorphism to create a single method that can be used to print different types of objects. This can save us a lot of time and code duplication.

Types of Polymorphism in Java

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.

  • Compile time polymorphism occurs when the compiler knows which method to call at compile time. This is typically achieved through method overloading.
  • Runtime polymorphism occurs when the compiler does not know which method to call at compile time. This is typically achieved through method overriding.

Compile time Polymorphism: Method Overloading

Method Overloading

Method overloading is a technique that allows us to define multiple methods with the same name, as long as the methods have different signatures. The signature of a method is the combination of its name, its parameters, and its return type.

For example, the following code defines two methods with the same name, print(). The first print() method takes an integer as a parameter, while the second print() method takes a string as a parameter.

Method Overloading example in Java:

public class Overloading {

  public void print(int number) {
    System.out.println(number);
  }

  public void print(String text) {
    System.out.println(text);
  }

  public static void main(String[] args) {
    Overloading overloading = new Overloading();
    overloading.print(10);
    overloading.print("Hello, world!");
  }
}

When the main() method calls the print() method with an integer argument, the compiler knows to call the first print() method. When the main() method calls the print() method with a string argument, the compiler knows to call the second print() method.

Runtime Polymorphism: Method Overriding

Polymorphism in Java Method Overriding

Method overriding is a technique that allows a subclass to redefine a method that was inherited from its superclass. When a subclass overrides a method, the subclass’s version of the method is called instead of the superclass’s version.

For example, the following code shows the method overriding example in Java. Where, it defines a superclass called Animal and a subclass called Dog. The Animal class has a method called makeSound(). The Dog class overrides the makeSound() method to print “Woof!”.

public class Animal {

  public void makeSound() {
    System.out.println("Animal sound");
  }
}

public class Dog extends Animal {

  @Override
  public void makeSound() {
    System.out.println("Woof!");
  }
}

public class Main {

  public static void main(String[] args) {
    Animal animal = new Animal();
    animal.makeSound();

    Dog dog = new Dog();
    dog.makeSound();
  }
}

When the main() method calls the makeSound() method on the animal object, the superclass’s version of the method is called. When the main() method calls the makeSound() method on the dog object, the subclass’s version of the method is called.

Conclusion

Polymorphism is a powerful feature of object-oriented programming that allows us to write more flexible and reusable code. In Java, polymorphism is achieved through two mechanisms: method overloading and method overriding.

Summary of Polymorphism in Java

  • Polymorphism is the ability of an object to behave differently in different contexts.
  • In Java, polymorphism is achieved through two mechanisms: method overloading and method overriding.
  • Method overloading allows us to define multiple methods with the same name, as long as the methods have different signatures.
  • Method overriding allows a subclass to redefine a method that was inherited from its superclass.
  • Polymorphism can be used to write more flexible and reusable code.

FAQs

What is polymorphism?

Polymorphism is the ability of an object to take on many forms. In short, polymorphism is a Many Form of a Single Object. In Java, polymorphism is achieved through inheritance and method overriding.

What are the two types of polymorphism in Java?

There are two types of polymorphism in Java:

Compile-time polymorphism: This type of polymorphism is also known as method overloading. It occurs when a class has multiple methods with the same name but different parameters. The compiler determines which method to call based on the type of arguments passed to it.

Runtime polymorphism: This type of polymorphism is also known as method overriding. It occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The compiler cannot determine which method to call until runtime, so it calls the method that is defined in the most specific class.

How can I achieve polymorphism in Java?

Polymorphism can be achieved in Java through inheritance and method overriding.

To achieve polymorphism through inheritance, a subclass can inherit methods and attributes from a superclass and also override or add new methods and attributes.

To achieve polymorphism through method overriding, a subclass can provide its own implementation of a method that is already defined in its superclass.

What are the benefits of using polymorphism in Java?

Polymorphism offers a number of benefits, including:

Code reuse: Polymorphism allows us to reuse code by writing methods that can be called on objects of different types.

Flexibility: Polymorphism makes our code more flexible by allowing us to change the behavior of our program at runtime.

Maintainability: Polymorphism makes our code more maintainable by making it easier to add new features and fix bugs.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments