Java Polymorphism

Polymorphism, which refers to the idea of “having many forms”, occurs when there is a hierarchy of classes related to each other through inheritance.

A call to a member method will cause a different implementation to be executed, depending on the type of object invoking the method.

Dog and Cat are classes that inherit from the Animal class. Each class has its own implementation of the makeSound() method.

Example:

class Animal {
  public void makeSound() {
    System.out.println("Grr...");
  }
}
class Cat extends Animal {
  public void makeSound() {
    System.out.println("Meow");
  }
}
class Dog extends Animal {
  public void makeSound() {
    System.out.println("Woof");
  }
Related Post