Java Reference ยท Object-Oriented Concepts
Inheritance in Java
How Java inheritance works: extends, overriding versus hiding, super calls, dynamic dispatch, final and sealed classes, and when to prefer composition.
Inheritance lets a class (the subclass) reuse and extend another class (the superclass) with the extends keyword. The subclass gets the accessible fields and methods of its parent, can add new ones, and can replace inherited instance methods by overriding them. Every class ultimately inherits from Object, and a class can extend exactly one other class (single inheritance of implementation). Interfaces provide the multiple-inheritance side of the language.
An object of the subclass is also an object of the superclass, so you can use it wherever the parent type is expected. When you call an overridden method through a parent-typed variable, Java runs the version belonging to the actual object: this is dynamic dispatch, the engine behind polymorphism.
class Animal { String sound() { return "..."; } } class Dog extends Animal { @Override String sound() { return "Woof"; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Rules
| Topic | Rule |
|---|---|
extends |
One superclass only. Omitted means Object. |
| Overriding | Same name and parameter types; return type the same or a subtype; access not narrower; no broader checked exceptions. Use @Override so the compiler checks you. |
static methods and fields |
Hidden, not overridden: the declared type picks which one you get. |
super.method() |
Calls the parent's implementation from an override. |
final |
A final method cannot be overridden; a final class cannot be extended. |
sealed |
sealed ... permits A, B limits which classes may extend (Java 17). See sealed classes. |
abstract |
An abstract class may leave methods unimplemented; subclasses must supply them. |
| Constructors | Not inherited. Each constructor calls a superclass constructor first (constructors). |
| Private members | Not accessible in the subclass, though they exist in the object. |
Usage notes
- Use inheritance for "is-a", composition for "has-a". A
Stackis not a kind ofVectorjust because it reuses its code. Holding a collaborator in a field is more flexible and does not leak the parent's whole API. - Design for extension or forbid it. Classes not meant to be extended should be
final, sealed or have only private constructors. Overridable methods that call each other create fragile coupling. - Liskov substitution. A subclass must honor the parent's contract. If code that works with a
Rectanglebreaks for aSquare, the hierarchy is wrong. - Calling overridable methods in constructors is unsafe because the subclass is not initialized yet (initialization order).
equalsacross subclasses is hard. Keep it symmetric; considerfinalor composition for value types.- Casting. Upcasting is implicit and safe; downcasting needs a cast and may throw
ClassCastException. Preferinstanceofpatterns. - Interfaces with
defaultmethods share behavior without the single-parent limit, but conflicts must be resolved explicitly withInterface.super.method(). - Method call cost. Virtual dispatch is cheap; the JIT inlines monomorphic calls, so do not avoid inheritance for speed.