</>
Skip to content
Java lessons (31/47)

Java — Polymorphism

What is polymorphism?

One interface, multiple implementations.

Method overriding

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Meow");
    }
}

Runtime polymorphism

Animal myAnimal = new Dog();
myAnimal.sound();  // Bark

myAnimal = new Cat();
myAnimal.sound();  // Meow

Mini Practice

  1. Override methods
  2. Use parent reference
  3. Call overridden methods
  4. Practice polymorphism

Up Next

Continue with Abstraction - Abstract classes.

Related Topics

Frequently Asked Questions about Polymorphism

What is Polymorphism in Java?

Polymorphism is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Polymorphism?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Polymorphism.

Why is Polymorphism important in Java?

Polymorphism is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.