← Java Programming learning path
Java Programming · Module 6 of 10

Inheritance and polymorphism

Reuse and extend classes with extends, override behaviour, and use one interface for many forms.

📖 Key ideas

  1. class Dog extends Animal: Dog inherits Animal's fields and methods.
  2. super calls the parent constructor or method.
  3. @Override replaces a parent method in the child.
  4. Polymorphism: an Animal variable can hold a Dog; the Dog version of a method runs.
  5. Java allows single class inheritance only.
💡 Real-life example: A smartphone inherits from phone: it can still call, but it also overrides the screen with touch.
extendsInherits from a class
superRefers to the parent class
@OverrideMarks a method that replaces a parent method
PolymorphismOne reference, many forms