Unravel โบ Java concepts โบ OOP and types
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
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".
- 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@Override asks the compiler to check that you really override somethingWithout it a typo in the name silently creates a new method.
- 3super.speak() calls the parent's version from inside the override; super(args) in a constructor builds the parent part of the object first
- 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.
- Inheritance is the strongest coupling in Java: a change in the parent can break every child
- Fields are not overridden, they are hidden
- The JVM keeps a method table per class
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; }}- line 1 uses , the core of Inheritance, extends and super
- line 2 uses , the core of Inheritance, extends and super
- line 3 uses , the core of Inheritance, extends and super
- line 4 uses , the core of Inheritance, extends and super
- line 5 uses , the core of Inheritance, extends and super
- 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:
- Annotations:
@Override - Method calls:
super.โฆ() - Language constructs:
ctor-chain
Part of a learning path
- Object-oriented Java in depth ยท previous: Classes, objects and constructors ยท next: Abstract classes
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.