Inheritance in Java with extends Keyword
Fast recall
Quick revision
Core answer
extends. The subclass inherits accessible members, can add members, and can override eligible instance methods. Constructors are not inherited, and private members are not directly accessible in the subclass. Use inheritance for a genuine substitutable IS-A relationship, not only to reuse code.Whether you can implement inheritance with extends and know it's an IS-A relationship aimed at reuse and polymorphism.
Using inheritance just to reuse code when the IS-A relationship doesn't truly hold.
Run the IS-A test before using extends, mention @Override and super, and note Java has no multiple inheritance of classes.
Interview-ready guide
Interview answer
The whole point of inheritance is to avoid writing the same code twice. If several classes share behaviour — every animal can eat — you put that method once in a parent class, and any class that extends it picks it up automatically. The parent is the superclass, the child the subclass, and extends links them; the child gets all non-private members, can add its own, and can override inherited ones to specialise.
Before I reach for extends I run the IS-A test — I say the relationship out loud. A dog is an animal, so inheritance fits. A car is not an engine, it has one, so that's composition. People get burned skipping that test and using inheritance purely for reuse: you inherit an API that doesn't belong and a rigid hierarchy that's hard to change.
The other payoff is polymorphism — an Animal reference pointing at a Dog still calls Dog's overridden method at runtime. Java allows only single class inheritance because of the diamond problem, though a class can implement as many interfaces as it needs. So my rule is simple: run the IS-A test first, and if it fails, reach for composition.
Inheritance in Java with extends Keyword
Direct Answer
Inheritance declares that one class is a subtype of another with extends. The subclass inherits accessible members, can add members, and can override eligible instance methods. Constructors are not inherited, and private members are not directly accessible in the subclass. Use inheritance for a genuine substitutable IS-A relationship, not only to reuse code.
Code Example
// Parent class — the shared blueprint
class Animal {
protected String name; // field available to subclasses
Animal(String name) {
this.name = name; // constructor sets the name
}
void eat() {
System.out.println(name + " is eating"); // shared behaviour
}
String sound() {
return "..."; // default; subclasses will override
}
}
// Child class — inherits everything above, then adds its own
class Dog extends Animal { // Dog IS-A Animal ✓
Dog(String name) {
super(name); // call parent constructor first
}
@Override // tells compiler: intentional override
String sound() {
return "Woof"; // specialised version of sound()
}
void fetch() {
System.out.println(name + " fetches the ball!"); // Dog-only method
}
}
// Usage
public class Main {
public static void main(String[] args) {
Dog rex = new Dog("Rex");
rex.eat(); // inherited from Animal → Rex is eating
System.out.println(rex.sound()); // overridden → Woof
rex.fetch(); // Dog-only → Rex fetches the ball!
Animal a = rex; // polymorphism: Dog treated as Animal
System.out.println(a.sound()); // still Woof — runtime dispatch
}
}
Common Mistakes & Pitfalls
- Using
extendsjust for code reuse when IS-A doesn't hold — you inherit the parent's entire public API, including methods that make no sense on the child, and lock yourself into a rigid hierarchy that's painful to change later. - Forgetting
@Overrideon an overridden method — a small typo in the signature silently creates a brand-new method instead of overriding, so the parent's version still runs and the compiler never warns you. - Skipping
super(args)in the subclass constructor — when the parent has only a parameterized constructor, the compiler's implicitsuper()call fails and you get a confusing 'no default constructor' error. - Trying to extend two classes — Java forbids it because of the diamond problem; reach for interfaces when you need a class to take on multiple types.
Check your understanding
Answer these without looking back at the article.
- What is the difference between IS-A and HAS-A relationships?
- Which members of a parent class are inherited by a subclass?
- Why does Java not support multiple inheritance for classes?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.