Inheritance
Inheritance is an object-oriented programming (OOP) concept where one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass). It promotes code reuse, extensibility, and hierarchical design.
This is a high-frequency interview topic and foundational for understanding polymorphism and method overriding.
What Is Inheritance?
- One class extends another class
- Child class inherits fields and methods
- Enables IS-A relationship
- Improves code reuse and maintainability
class Parent {
int x = 10;
}
class Child extends Parent {
int y = 20;
}
Here: Child IS-A Parent
Why Inheritance Is Important
- Avoids code duplication
- Supports extensibility
- Enables polymorphism
- Improves maintainability
- Represents real-world hierarchies
Basic Syntax
class SubClass extends SuperClass {
// additional members
}
Example: Simple Inheritance
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited
d.bark(); // own method
}
}
Types of Inheritance in Java
1. Single Inheritance
One child inherits from one parent.
class B extends A { }
✔ Supported
2. Multilevel Inheritance
Chain of inheritance.
class A { }
class B extends A { }
class C extends B { }
✔ Supported
3. Hierarchical Inheritance
Multiple children inherit from one parent.
class A { }
class B extends A { }
class C extends A { }
✔ Supported
4. Multiple Inheritance (Classes)
class C extends A, B { } // ❌ Not allowed
❌ Not supported using classes
✔ Achieved using interfaces
extends Keyword
- Used to inherit a class
- Only one class can be extended
- Establishes parent-child relationship
What Is Inherited and What Is Not
Inherited:
- Public members
- Protected members
- Default members (same package)
Not Inherited:
- Private members
- Constructors
Constructor Behavior in Inheritance
class A {
A() {
System.out.println("A constructor");
}
}
class B extends A {
B() {
System.out.println("B constructor");
}
}
Output:
A constructor
B constructor
- Parent constructor executes first
- super() is called implicitly
super Keyword (Preview)
- Access parent class members
- Call parent constructor
super.methodName();
super();
Method Overriding (Preview)
Child class provides specific implementation of parent method.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
✔ Runtime polymorphism
Inheritance vs Composition (Design Insight)
| Aspect | Inheritance | Composition |
|---|---|---|
| Relationship | IS-A | HAS-A |
| Coupling | Tight | Loose |
| Flexibility | Less | More |
Best Practice: Prefer composition unless true IS-A relationship exists.
Inheritance Examples and Rules (Quick Reference)
1. Basic Single Inheritance
class A {
void show() {
System.out.println("Class A");
}
}
class B extends A {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
Explanation: B inherits methods of A.
Output: Class A
2. Inheriting Variables
class A {
int x = 10;
}
class B extends A {
public static void main(String[] args) {
B b = new B();
System.out.println(b.x);
}
}
Explanation: Instance variables are inherited.
Output: 10
3. Method Overriding (Runtime Polymorphism)
class A {
void show() {
System.out.println("Parent");
}
}
class B extends A {
void show() {
System.out.println("Child");
}
public static void main(String[] args) {
A a = new B();
a.show();
}
}
Explanation: Method resolved at runtime.
Output: Child
4. Overriding Rules (Same Signature)
class A {
void test() {}
}
class B extends A {
// void test(int x) {} // ❌ Overloading, not overriding
}
Explanation: Signature must be same for overriding.
5. Using super to Access Parent Method
class A {
void show() {
System.out.println("A");
}
}
class B extends A {
void show() {
super.show();
System.out.println("B");
}
public static void main(String[] args) {
new B().show();
}
}
Explanation: super calls parent method.
Output:
A
B
6. Constructor Execution Order
class A {
A() {
System.out.println("A constructor");
}
}
class B extends A {
B() {
System.out.println("B constructor");
}
public static void main(String[] args) {
new B();
}
}
Explanation: Parent constructor executes first.
Output:
A constructor
B constructor
7. Parameterized Parent Constructor
class A {
A(int x) {
System.out.println(x);
}
}
class B extends A {
B() {
super(10);
}
public static void main(String[] args) {
new B();
}
}
Explanation: Explicit super() required.
Output: 10
8. Multilevel Inheritance
class A {
void a() { System.out.println("A"); }
}
class B extends A {
void b() { System.out.println("B"); }
}
class C extends B {
public static void main(String[] args) {
C c = new C();
c.a();
c.b();
}
}
Explanation: Chain inheritance.
Output:
A
B
9. Hierarchical Inheritance
class A {
void show() {
System.out.println("A");
}
}
class B extends A {}
class C extends A {}
class Test {
public static void main(String[] args) {
new B().show();
new C().show();
}
}
Explanation: One parent, multiple children.
Output:
A
A
10. Multiple Inheritance ❌ (Classes)
class A {}
class B {}
// class C extends A, B {} // ❌ Not allowed
Explanation: Java does not support multiple inheritance with classes.
11. Multiple Inheritance via Interfaces
interface A {
void a();
}
interface B {
void b();
}
class C implements A, B {
public void a() { System.out.println("A"); }
public void b() { System.out.println("B"); }
public static void main(String[] args) {
C c = new C();
c.a();
c.b();
}
}
Explanation: Achieved using interfaces.
Output:
A
B
12. Inheritance with final Method
class A {
final void show() {
System.out.println("Final Method");
}
}
class B extends A {
// void show() {} // ❌ Cannot override
}
Explanation: final methods cannot be overridden.
13. final Class Cannot Be Inherited
final class A {}
// class B extends A {} // ❌ Compile-time error
Explanation: final prevents inheritance.
14. Method Hiding (Static Methods)
class A {
static void show() {
System.out.println("A");
}
}
class B extends A {
static void show() {
System.out.println("B");
}
public static void main(String[] args) {
A.show();
B.show();
}
}
Explanation: Static methods are hidden, not overridden.
Output:
A
B
15. Field Hiding
class A {
int x = 10;
}
class B extends A {
int x = 20;
public static void main(String[] args) {
A a = new B();
System.out.println(a.x);
}
}
Explanation: Variables resolved at compile time.
Output: 10
16. Upcasting (Implicit)
class A {
void show() {
System.out.println("A");
}
}
class B extends A {}
class Test {
public static void main(String[] args) {
A a = new B(); // upcasting
a.show();
}
}
Explanation: Child object referenced by parent.
Output: A
17. Downcasting (Explicit)
class A {}
class B extends A {
void test() {
System.out.println("B method");
}
}
class Test {
public static void main(String[] args) {
A a = new B();
B b = (B) a;
b.test();
}
}
Explanation: Explicit cast required.
Output: B method
18. Invalid Downcasting
class A {}
class B extends A {}
class Test {
public static void main(String[] args) {
A a = new A();
// B b = (B) a; // Runtime ClassCastException
}
}
Explanation: Object must actually be child type.
19. instanceof with Inheritance
class A {}
class B extends A {}
class Test {
public static void main(String[] args) {
A a = new B();
System.out.println(a instanceof B);
}
}
Explanation: Runtime type check.
Output: true
20. Interview Summary – Inheritance
class A {
void show() {
System.out.println("A");
}
}
class B extends A {
void show() {
System.out.println("B");
}
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
Explanation: IS-A relationship, method overriding, runtime polymorphism.
Output: B
Common Beginner Mistakes
- Using inheritance where IS-A does not apply
- Accessing private members directly
- Forgetting parent constructor call
- Overusing inheritance
- Confusing inheritance with interfaces
Interview-Ready Answers
Short Answer
Inheritance allows one class to acquire the properties and behaviors of another class.
Detailed Answer
In Java, inheritance is achieved using the extends keyword, allowing a subclass to reuse fields and methods of a superclass. Java supports single, multilevel, and hierarchical inheritance, but not multiple inheritance using classes. Inheritance enables code reuse, extensibility, and polymorphism.
Key Takeaway
Inheritance builds hierarchical relationships and enables code reuse, but it should be used judiciously to avoid tight coupling and design rigidity.