Inheritance in Java
In this lesson: Learn single, multilevel and hierarchical inheritance in Java with extends, super and examples with output. In Hindi & English.
What is inheritance
Inheritance lets one class reuse the fields and methods of another. The child class inherits from a parent class using extends, getting everything the parent has plus its own additions. This avoids repeating shared code and models "is-a" relationships (a Dog is an Animal).
class Dog extends Animal means Dog inherits everything from Animal.Using extends
class Animal {
void eat() { System.out.println("eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("woof"); }
}
// use:
Dog d = new Dog();
d.eat(); // inherited from Animal
d.bark(); // its own
woof
Dog extends Animal gives Dog the eat() method for free, and Dog adds its own bark(). One keyword unlocks all the parent's abilities.
Overriding a method
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Cat extends Animal {
void sound() { // override
System.out.println("Meow");
}
}
// use:
new Cat().sound();
A child can replace an inherited method by defining its own with the same name — this is overriding. For a Cat object, the child's sound() runs instead of the parent's.
super in inheritance
class Person {
Person(String name) { System.out.println("Person: " + name); }
}
class Student extends Person {
Student(String name) {
super(name); // call parent constructor
System.out.println("Student created");
}
}
new Student("Aman");
Student created
super(name) calls the parent's constructor first, then the child continues. super also calls a parent's overridden method with super.method().
Types of inheritance
- Single: one child, one parent (Dog → Animal).
- Multilevel: a chain (Puppy → Dog → Animal).
- Hierarchical: many children of one parent (Dog and Cat both → Animal).
Java supports these with classes. It does not support multiple inheritance of classes (one child, many parents).
Why no multiple inheritance
Java does not let a class extend two classes at once, to avoid the diamond problem: if both parents had a method with the same name, the child could not tell which to use. To combine behaviour from multiple sources, Java uses interfaces, which sidestep this ambiguity.
Common mistakes
- Wrong parent name.
extends Animalneeds Animal to exist and be spelled correctly. - Overriding by accident. Reusing a parent method name replaces it — rename if unintended.
- Forgetting
super(...)for parent setup. A parameterised parent constructor must be called. - Trying to extend two classes. Not allowed — use interfaces instead.
Summary
- Inheritance reuses a parent's fields and methods via
extends. - A child gets the parent's members plus its own.
- Overriding replaces an inherited method with the same name.
supercalls the parent's constructor or overridden method.- Java allows single, multilevel and hierarchical — not multiple inheritance of classes.
Next, use this and super in depth, or combine behaviour with abstraction and interfaces.
Frequently Asked Questions
What is inheritance in Java?
extends keyword. The child gets everything the parent has, plus anything it adds, which avoids repeating code.What is method overriding?
What are the types of inheritance in Java?
Why does Java not allow multiple inheritance of classes?
What does the super keyword do in inheritance?
super lets a child access its parent — calling the parent's constructor with super(...), or the parent's overridden method with super.method(). It connects the child back to the parent it extends.