Java Inheritance
Inheritance lets a subclass reuse and specialize behavior and state defined by a superclass. In Java, a class can extend one superclass and inherit accessible members while adding or overriding behavior.
Inheritance models an "is-a" relationship between classes. A subclass uses `extends` to derive from a superclass, gaining access to inherited members according to Java's access rules and its own declared members.
A subclass can override an inherited instance method by providing a compatible implementation. When an overridden method is invoked through a superclass reference, Java uses dynamic method dispatch to select the subclass implementation at runtime.
Constructors are not inherited. When a subclass object is created, its constructor must ultimately invoke a superclass constructor, either explicitly with `super(...)` or implicitly through the no-argument superclass constructor when available.
Java supports single class inheritance: each class has one direct superclass. Multiple types can still be represented through interfaces, allowing a class to implement multiple interfaces without inheriting implementation from multiple classes.
Inheritance is useful when a subtype genuinely satisfies the superclass's abstraction. If the relationship mainly exists to reuse implementation, composition is often a better design because it reduces coupling between classes.
Key points
- Use `extends` for class inheritance.
- A subclass can add members and override inherited instance methods.
- Constructors are not inherited; superclass construction happens through `super(...)`.
- Java permits one direct superclass per class.
- Overridden instance methods use runtime dynamic dispatch.
- Prefer inheritance for a genuine subtype relationship; consider composition for simple code reuse.
Interview questions
What is inheritance in Java?
Inheritance allows one class to derive from another using `extends`. The subclass can use accessible inherited members and can add or specialize behavior. It represents an is-a relationship between the subclass and superclass. Java allows a class to have only one direct superclass.
Are constructors inherited in Java?
No, constructors are not inherited as members of a subclass. When constructing a subclass object, the subclass constructor must initialize the superclass portion of the object. It can call a particular superclass constructor with `super(...)`, or Java may insert a no-argument `super()` call when appropriate.
What happens when a subclass overrides a method?
The subclass supplies its own implementation of an inherited instance method with a compatible signature. When the method is called on an object, Java selects the override according to the object's runtime class. This is dynamic method dispatch. Using a superclass reference does not prevent the subclass implementation from being selected.
Why might composition be preferred over inheritance?
Composition can reduce coupling because a class delegates work to contained objects instead of becoming tightly tied to a superclass hierarchy. It also allows behavior to be changed by replacing or configuring collaborators. Inheritance is appropriate when the subtype relationship is semantically meaningful. The choice should therefore follow the domain relationship rather than reuse alone.
How does Java handle multiple inheritance?
A Java class can extend only one class, so it does not support multiple inheritance of classes. A class can implement multiple interfaces, which provides a way to conform to several types. Interfaces can also contain default methods, so conflicts may need explicit resolution when multiple interfaces provide compatible defaults. This design separates class inheritance from implementing multiple contracts.