Start Coding Now

Polymorphism in Java - Types & Examples

Master polymorphism in Java. Learn about compile-time (overloading) and runtime (overriding) polymorphism.

What is Polymorphism?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

It allows us to perform a single action in different ways.

Types:

  • Compile-time: Method Overloading.
  • Runtime: Method Overriding.
  • class Animal {
      public void animalSound() { System.out.println("The animal makes a sound"); }
    }

    class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } }

    class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } }

    Frequently Asked Questions