Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
Chaturmind
← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
HomeLearnJavaJava Core FundamentalsObject-Oriented Programming
✓ FreeBeginner· 10 min read

Inheritance and Polymorphism

Extend behaviour with inheritance and achieve flexibility with polymorphism.

Published January 16, 2025


Inheritance and Polymorphism

Inheritance lets a class reuse behaviour from a parent class. Polymorphism lets you write code that works with multiple types through a shared interface.

public class Animal {
    protected String name;

    public Animal(String name) { this.name = name; }

    public String sound() { return "..."; }
}

public class Dog extends Animal {
    public Dog(String name) { super(name); }

    @Override
    public String sound() { return "Woof"; }
}

public class Cat extends Animal {
    public Cat(String name) { super(name); }

    @Override
    public String sound() { return "Meow"; }
}

Runtime Polymorphism

List<Animal> animals = List.of(new Dog("Rex"), new Cat("Mimi"));
for (Animal a : animals) {
    System.out.println(a.name + " says " + a.sound());
}
// Rex says Woof
// Mimi says Meow

The correct sound() method is selected at runtime based on the actual object type — this is called dynamic dispatch.

final, abstract, and sealed

  • final class — cannot be extended
  • abstract class — cannot be instantiated; subclasses must implement abstract methods
  • sealed class (Java 17+) — only permitted subclasses can extend it

Interview Tip

Distinguish method overriding (runtime polymorphism, @Override) from method overloading (compile-time, same name different parameters). Interviewers ask about this constantly.

Previous

Classes and Objects

Next

Interfaces and Abstract Classes

AI Tutor

Lesson: Inheritance and Polymorphism

Quick actions

AI responses can be inaccurate. Verify critical information.