What does the extends keyword do in Java, and what does it mean that Java only allows single class inheritance?
answer
- extends = one direct parent
- single class inheritance → no diamond
- interfaces give multiple behavior
- no extends → implicitly extends Object
- is-a relationship enables polymorphism
basics
~10 sextends makes one class a subclass of another, so the subclass inherits the parent's fields and methods. Java allows a class to extend only one direct parent class (single inheritance).
solid answer
~40 sThe extends keyword declares a class as a subclass of exactly one superclass: 'class Dog extends Animal'. The subclass inherits all accessible (non-private) fields and methods and can add its own or override inherited ones. Java enforces single class inheritance: a class can have only one direct superclass, which avoids the diamond ambiguity that multiple class inheritance creates. To get behavior from multiple sources you implement multiple interfaces (which can carry default methods) rather than extending multiple classes. Every class without an explicit extends clause implicitly extends Object, so the inheritance graph is always a single rooted tree. Inheritance models an 'is-a' relationship and enables polymorphism: a reference of the superclass type can hold any subclass instance.
code
java · 14 linesclass Animal {
void breathe() { System.out.println("breathing"); }
}
class Dog extends Animal { // single parent
void bark() { System.out.println("woof"); }
}
// class Dog extends Animal, Pet // COMPILE ERROR: no multiple class inheritance
Dog d = new Dog();
d.breathe(); // inherited from Animal
d.bark(); // declared in Dog
Animal a = d; // polymorphism: Dog is-a Animalgo deeper
Knows extends creates a subclass, that only one parent class is allowed, and that the subclass gets the parent's accessible members.
Explains why single inheritance exists (diamond problem), contrasts extends vs implements, and knows every class extends Object implicitly.
Discusses inheritance vs composition trade-offs, default-method conflict resolution across interfaces, and how polymorphism follows from is-a.
Frames inheritance as a coupling decision in API/architecture design, weighs fragile-base-class risks, and guides teams toward composition or sealed hierarchies where appropriate.
## What 'inheritance' means **Inheritance** is a mechanism where one class (the **subclass** or **child**) is defined in terms of another (the **superclass** or **parent**), automatically acquiring the parent's members. A **member** is a field (variable) or method (function) declared in a class. The point is reuse and modeling an **is-a** relationship: a `Dog` *is an* `Animal`. ## The `extends` keyword In Java you create this relationship with `extends`: ```java class Animal { void breathe() {} } class Dog extends Animal { void bark() {} } ``` Here `Dog` is a subclass of `Animal`. A `Dog` object now has both `bark()` (its own) and `breathe()` (inherited). The subclass inherits every member that is **accessible** to it — that means everything except `private` members (private members exist in the parent object but cannot be referenced by name from the subclass). ## Single (class) inheritance Java allows a class to list **only one** class after `extends`. This is **single inheritance**. You cannot write `class C extends A, B`. The reason is the **diamond problem**: if a class could inherit from two parents that both define a method `foo()` with different bodies, the compiler would not know which one the child should use. Languages that allow multiple class inheritance (like C++) need extra rules to resolve this. Java sidesteps it for classes entirely. ## How Java gives you 'multiple' behavior anyway A class can `implements` any number of **interfaces**. An interface is a contract of method signatures; since Java 8 it can also supply **default methods** (method bodies). So you get behavior from many sources via interfaces while keeping single *class* inheritance: ```java class Duck extends Bird implements Swimmer, Flyer { } ``` If two interfaces provide conflicting default methods, Java forces *you* to override and resolve it explicitly — so there is no silent ambiguity. ## Object: the root of everything If a class has no `extends` clause, the compiler implicitly inserts `extends Object`. Therefore **every** class is ultimately a descendant of `java.lang.Object`, and the whole set of classes forms one tree rooted at `Object`. That is why every object has methods like `toString()`, `equals()`, and `hashCode()`. ## Why it matters: polymorphism Because `Dog` is-a `Animal`, a variable typed `Animal` can refer to a `Dog`: `Animal a = new Dog();`. This **polymorphism** is the practical payoff of inheritance — you write code against the general type and it works for any subtype. ## Common boundaries - `final` classes cannot be extended (e.g. `String`). - A subclass cannot reduce the visibility of an overridden method. - Constructors are **not** inherited.
- If Java forbids multiple class inheritance, how do you reuse code from several sources?Implement multiple interfaces (which can carry default method bodies) or use composition — hold instances of other classes as fields and delegate to them.
- Can you extend a final class?No. A class marked final cannot be subclassed; that is why classes like String are final.
A family tree where every person has exactly one biological parent line you can name — you can't list two fathers — but you can join many clubs (interfaces) for extra skills.
saying these in an interview costs you the question
- Saying Java supports multiple class inheritance — it does not (only multiple interfaces)
- Claiming private members are inherited and usable by name in the subclass
- Thinking constructors are inherited
- Confusing extends (classes) with implements (interfaces)