BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Inheritance, extends and super

class Dog extends Animal makes Dog a specialised Animal: it inherits the fields and methods and may replace (override) some of them.

Learn these first

At a glance

Mental model

A family recipe handed down: the child keeps the recipe, changes one ingredient, and can still say "do it the way mum does, then add chili".

What happens, in order
  1. 1A class has exactly one parent (extends)Everything that is not private is inherited. A variable of the parent type can hold a child object, and the child's overriding method runs: that is polymorphism.
  2. 2@Override asks the compiler to check that you really override somethingWithout it a typo in the name silently creates a new method.
  3. 3super.speak() calls the parent's version from inside the override; super(args) in a constructor builds the parent part of the object first
  4. 4Overriding needs the same name and parameter types and a compatible return type, and cannot reduce visibilityOverloading (same name, different parameters) is resolved at compile time, overriding at run time.
Remember
Watch out
Forgetting @Override, so a misspelled method never gets calledCalling an overridable method from the parent constructorUsing inheritance only to reuse code, when a field would do
Example (highlighted lines explain it)
public class Animal {  protected final String name;  public Animal(String name) { this.name = name; }  public String speak() { return "..."; }}class Dog extends Animal {  public Dog(String name) { super(name); }  @Override  public String speak() { return super.speak() + " Woof from " + name; }}
  1. line 1 uses , the core of Inheritance, extends and super
  2. line 2 uses , the core of Inheritance, extends and super
  3. line 3 uses , the core of Inheritance, extends and super
  4. line 4 uses , the core of Inheritance, extends and super
  5. line 5 uses , the core of Inheritance, extends and super
  6. line 6 uses , the core of Inheritance, extends and super
Read the full explanation (about 2 min)

class Dog extends Animal makes Dog a specialised Animal: it inherits the fields and methods and may replace (override) some of them. super reaches the parent version.

A mental model

A family recipe handed down: the child keeps the recipe, changes one ingredient, and can still say "do it the way mum does, then add chili".

How it works

A class has exactly one parent (extends). Everything that is not private is inherited. A variable of the parent type can hold a child object, and the child's overriding method runs: that is polymorphism.

@Override asks the compiler to check that you really override something. Without it a typo in the name silently creates a new method.

super.speak() calls the parent's version from inside the override; super(args) in a constructor builds the parent part of the object first.

Overriding needs the same name and parameter types and a compatible return type, and cannot reduce visibility. Overloading (same name, different parameters) is resolved at compile time, overriding at run time.

Going deeper

Inheritance is the strongest coupling in Java: a change in the parent can break every child. Prefer composition (a field holding a collaborator) unless the child truly "is a" parent in every situation.

Fields are not overridden, they are hidden. Parent p = new Child(); p.name reads the parent's field even if Child declares its own name. Methods are the only thing that dispatches dynamically.

final on a class or a method forbids extending or overriding. Make classes final unless you designed them for inheritance.

Under the hood

The JVM keeps a method table per class. invokevirtual looks up the method in the table of the real class of the object, which is why the child's override wins even when the variable has the parent type. private, static and final methods and constructors are bound directly.

Common mistakes

  • Forgetting @Override, so a misspelled method never gets called.
  • Calling an overridable method from the parent constructor.
  • Using inheritance only to reuse code, when a field would do.

How Unravel recognises it

When you paste code, Unravel looks for these signs of inheritance, extends and super. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Inheritance, extends and super?

class Dog extends Animal makes Dog a specialised Animal: it inherits the fields and methods and may replace (override) some of them. super reaches the parent version.

What are the most common mistakes with Inheritance, extends and super?

Forgetting @Override, so a misspelled method never gets called. Calling an overridable method from the parent constructor. Using inheritance only to reuse code, when a field would do.

How can I see Inheritance, extends and super in real code?

Paste your own Java into Unravel and it will point out where inheritance, extends and super appears, explain how it works at the detail level you choose, and draw the flow of the code around it.