Inheritance in Java: Types and Examples (extends, super)
Inheritance lets a child class reuse parent code using extends. See single, multilevel and hierarchical types, super keyword, and why multiple inheritance is banned.
What is inheritance?
extends keyword. The child reuses parent code and can add its own — the "is-a" relationship: a Dog is an Animal.class Animal { // parent (superclass)
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal { // child (subclass)
void bark() { System.out.println("Barking!"); }
}
Dog d = new Dog();
d.eat(); // inherited from Animal - no rewrite needed
d.bark(); // Dog's own method
Types of inheritance in Java
// 1. SINGLE: A -> B
class B extends A { }
// 2. MULTILEVEL: A -> B -> C
class C extends B { } // C gets A's + B's members
// 3. HIERARCHICAL: A -> B, A -> C
class B extends A { }
class C extends A { } // two children, one parent
class C extends A, B) — compile error. Reason: the diamond problem — if A and B both have show(), which one does C inherit? Java avoids the ambiguity; multiple inheritance is achieved through interfaces instead.The super keyword
class Animal {
String name = "Animal";
Animal() { System.out.println("Animal constructor"); }
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
String name = "Dog";
Dog() {
super(); // parent constructor (implicit anyway)
System.out.println("Dog constructor");
}
void sound() {
super.sound(); // parent's version
System.out.println("Bark! My parent field: " + super.name);
}
}
new Dog().sound();
Three uses of super: super() calls the parent constructor (always runs first), super.method() calls the parent's version, super.field reads the parent's hidden field.
Real-world example: school system
class Person {
String name;
Person(String n) { name = n; }
void introduce() { System.out.println("I am " + name); }
}
class Teacher extends Person {
String subject;
Teacher(String n, String s) { super(n); subject = s; }
void teach() { System.out.println(name + " teaches " + subject); }
}
class Student extends Person {
int roll;
Student(String n, int r) { super(n); roll = r; }
}
new Teacher("Gagan Sir", "Computer").teach();
new Student("Aman", 101).introduce();
Person's code is written once; Teacher and Student both reuse it. This is the entire point: code reusability + logical hierarchy.
Inheritance क्या है?
extends keyword के ज़रिए parent class के fields और methods पा लेती है. Child parent का code reuse करता है और अपना जोड़ सकता है — "is-a" relationship: Dog एक Animal है.class Animal { // parent (superclass)
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal { // child (subclass)
void bark() { System.out.println("Barking!"); }
}
Dog d = new Dog();
d.eat(); // Animal से inherited - दोबारा लिखने की ज़रूरत नहीं
d.bark(); // Dog का अपना method
Java में inheritance के types
// 1. SINGLE: A -> B
class B extends A { }
// 2. MULTILEVEL: A -> B -> C
class C extends B { } // C को A के + B के members मिले
// 3. HIERARCHICAL: A -> B, A -> C
class B extends A { }
class C extends A { } // दो children, एक parent
class C extends A, B) — compile error. वजह: diamond problem — अगर A और B दोनों में show() हो, तो C किसका inherit करे? Java इस ambiguity से बचता है; multiple inheritance interfaces से achieve होती है.super keyword
class Animal {
String name = "Animal";
Animal() { System.out.println("Animal constructor"); }
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
String name = "Dog";
Dog() {
super(); // parent constructor (वैसे implicit है)
System.out.println("Dog constructor");
}
void sound() {
super.sound(); // parent का version
System.out.println("Bark! Parent field: " + super.name);
}
}
new Dog().sound();
super के तीन uses: super() parent constructor call करता है (हमेशा पहले चलता है), super.method() parent का version, super.field parent की hidden field.
Real-world example: school system
class Person {
String name;
Person(String n) { name = n; }
void introduce() { System.out.println("I am " + name); }
}
class Teacher extends Person {
String subject;
Teacher(String n, String s) { super(n); subject = s; }
void teach() { System.out.println(name + " teaches " + subject); }
}
class Student extends Person {
int roll;
Student(String n, int r) { super(n); roll = r; }
}
new Teacher("Gagan Sir", "Computer").teach();
new Student("Aman", 101).introduce();
Person का code एक बार लिखा; Teacher और Student दोनों reuse करते हैं. यही पूरा point है: code reusability + logical hierarchy.
Frequently Asked Questions
What is inheritance in Java?
Inheritance is the mechanism where a child class acquires the fields and methods of a parent class using extends, enabling code reuse and an is-a relationship.
Why does Java not support multiple inheritance with classes?
Because of the diamond problem: if two parents define the same method, the child inherits an ambiguous version. Java offers multiple inheritance through interfaces instead.
What are the uses of the super keyword?
super() calls the parent constructor as the first statement, super.method() calls the parent version of an overridden method, and super.field accesses a parent field hidden by the child.