0% found this document useful (0 votes)
44 views6 pages

Java Inheritance Explained: Types & Syntax

Inheritance

Uploaded by

saikiransai9948
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

Java Inheritance Explained: Types & Syntax

Inheritance

Uploaded by

saikiransai9948
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance in Java

1. What is Inheritance?
• Inheritance is one of the core concepts of Object-Oriented Programming (OOP).
• In Java, Inheritance means one class (child class) acquiring the properties (fields) and behaviors
(methods) of another class (parent class).
• It helps in code reusability and method overriding.

2. Syntax of Inheritance
class Parent {
// fields and methods
}

class Child extends Parent {


// fields and methods
}
Here, Child class inherits from Parent class using the extends keyword.

3. Types of Inheritance in Java


Type Description

Single Inheritance One class inherits from one other class

Multilevel Inheritance Class inherits from a child class (chain-like)

Hierarchical Inheritance Multiple classes inherit from a single parent

Multiple Inheritance One class inherits from multiple classes (Not supported via classes)

Hybrid Inheritance Combination of two or more types (Handled using interfaces)

4. Single Inheritance
One child class inherits from one parent class. One child class inherits from one parent

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
class.
class Animal {
void eat() {
[Link]("Animal eats food");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog barks");
}
}
Dog inherits eat() method from Animal.

5. Multilevel Inheritance
One class inherits from a class which itself is inherited from another class.

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
class Animal {
void eat() {
[Link]("Animal eats food");
}
}

class Mammal extends Animal {


void walk() {
[Link]("Mammal walks");
}
}

class Dog extends Mammal {


void bark() {
[Link]("Dog barks");
}
}
Dog indirectly inherits eat() and walk() methods.

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
6. Hierarchical Inheritance
Multiple child classes inherit from one parent
class.

class Animal {
void eat() {
[Link]("Animal eats food");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog barks");
}
}

class Cat extends Animal {


void meow() {
[Link]("Cat meows");
}
}
Both Dog and Cat inherit eat() from Animal.

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
7. Multiple Inheritance (Classes)
• Java does not support multiple inheritance with classes to avoid ambiguity problems (known as
the Diamond Problem).
• Multiple Inheritance means a class trying to inherit from more than one class.
Example that is NOT allowed in Java:
class A {
void show() {
[Link]("A's show");
}
}

class B {
void show() {
[Link]("B's show");
}
}

// This is NOT allowed


class C extends A, B { // Error: Class cannot extend multiple classes
// Ambiguity: Which show() to inherit?
}
Result:
• Java compiler will throw an error:
Class C cannot extend multiple classes

Diamond Problem in Java


What is the Diamond Problem?
• It happens when one class inherits from two classes that have a common superclass.
• The problem is ambiguity:
If two parent classes have the same method, and a child class inherits both,
then which method should the child inherit?
Classic Diamond Shape Diagram:
A
/\
B C
\/
D
Where:
• Class B and Class C inherit from A
• Class D tries to inherit from both B and C
Problem: If A, B, and C all have a method display(), and D inherits from both B and C,
then which display() should D inherit? B's or C's?
→ AMBIGUITY!

Why Java does not allow Multiple Inheritance using Classes?


• To avoid ambiguity at runtime and compile time.
• Simplify the design — no confusion about which method to inherit.
• Prevent complexity — keeps Java simple and easy to understand.

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD
Interfaces and Multiple Inheritance
Java solves this problem using interfaces.
In interfaces, even if two interfaces have the same method signature, the implementing class must override the
method and provide its own definition — so no ambiguity.

Final Conclusion:
Situation Java Behavior

Multiple Inheritance using Classes Not allowed (to prevent Diamond Problem)

Multiple Inheritance using Interfaces Allowed (child must override methods)

Important Keywords:
Keyword Meaning

extends Used for class inheritance

implements Used for interface inheritance

super To call parent class constructor/method

this Current object reference

Quick Summary (Important Points)


• Java supports single, multilevel, and hierarchical inheritance via classes.
• Java does not support multiple inheritance via classes to avoid Diamond Problem.
• Java supports multiple inheritance via interfaces.
• Private members of the parent are not inherited.
• Constructors are not inherited, but the child can call them using super().

+91 6305693431, 9154641231, 8790732332


MIG 214, 2ND FLOOR, KPHB KUKATPALLY, HYDERABAD, 500072 | SRA EDUTECH PVT LTD

You might also like