0% found this document useful (0 votes)
4 views

5. Inheritance

The document outlines five types of inheritance in programming: single, multilevel, multiple, hierarchical, and hybrid inheritance. It provides definitions and examples for each type, highlighting how classes can extend one another in various configurations. Additionally, it includes sample code for single and multilevel inheritance, along with a note on Java's lack of support for multiple inheritance due to potential ambiguity.

Uploaded by

amitjobs1234
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)
4 views

5. Inheritance

The document outlines five types of inheritance in programming: single, multilevel, multiple, hierarchical, and hybrid inheritance. It provides definitions and examples for each type, highlighting how classes can extend one another in various configurations. Additionally, it includes sample code for single and multilevel inheritance, along with a note on Java's lack of support for multiple inheritance due to potential ambiguity.

Uploaded by

amitjobs1234
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
You are on page 1/ 5

5.

Inheritance Shekhar 1

TYPES OF INHERITANCE

There exist basically FIVE types of inheritance.

1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

1. SINGLE INHERITANCE

In single inheritance, one class extends one class only.

Eg. Class A{ }
Class B extends A{ }

2. MULTILEVEL INHERITANCE

It is one in which there exists only one base class, one derived class and multiple intermediate base classes.
In multilevel inheritance, the ladder of single inheritance increases. In multilevel, one-to-one ladder
increases. Multiple classes are involved in inheritance, but one class extends only one. The lowermost
subclass can make use of all its super classes' members.

Eg. Class A{ }
Class B extends A{ }
Class C extends B{ }

3. MULTIPLE INHERITANCE

In multiple inheritance, one class extends multiple classes. Java does not support multiple
inheritance but C++ supports.
Eg. Class A{ }
Class B{ }
Class C extends A,B{ }

*********** Software engineer at TATA CONSULTANCY SERVICES, mail to : [email protected], mob: 9681137876 ***********
5. Inheritance Shekhar 2

4. HIERARCHICAL INHERITANCE

In hierarchical inheritance, one class is extended by many subclasses. It is one-to-many relationship.

Eg. Class A{ }
Class B extends A{ }
Class C extends A{ }

5. HYBRID INHERITANCE

It is mixture of any two of above named inheritances.


In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance. A
hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using
interfaces. By using interface you can have multiple as well as hybrid inheritance in Java.

Eg. Class A{ }
Class B extends A{ }
Class C extends A{ }
Class D extends B{ }

PROGRAM 1: (single inheritance)

class A{
private int x=10,y=20;
public int z=30;
public void show(){
System.out.println(x+y);
}
}
class B extends A{
private int v=50;
public void sum(){
System.out.println(v+z);
}
}
class Main{
public static void main(String[] aa){
B ob=new B();
ob.show();
ob.sum();
}
} o/p : 30 80

*********** Software engineer at TATA CONSULTANCY SERVICES, mail to : [email protected], mob: 9681137876 ***********
5. Inheritance Shekhar 3

PROGRAM 2: (multilevel inheritance)

class A{
private int x=10,y=20;
public int z=30;
public void show(){
System.out.println(x+y);
}
}
class B extends A{
public int v=50;
public void sum(){
System.out.println(v+z);
}
}
class C extends B{
private int m=60;
public void display(){
System.out.println(m+v+z);
}
}
class Main{
public static void main(String[] aa){
C ob=new C();
ob.show();
ob.sum();
ob.display();
}
}

o/p : 30 80 140

PROGRAM 3: (multilevel inheritance)

class A{
public int z=30;
public void show(){
System.out.println("from A");
}
}
class B extends A{
public int v=50;
public void show(){
System.out.println("from B");
}
}

*********** Software engineer at TATA CONSULTANCY SERVICES, mail to : [email protected], mob: 9681137876 ***********
5. Inheritance Shekhar 4

class C extends B{
public void display(){
System.out.println(v+z);
}
}
class Main{
public static void main(String[] aa){
C ob=new C();
ob.show();
ob.display();
}
}
o/p : from B 80

PROGRAM 4: (multilevel inheritance)

class A{
public void show(){
System.out.println("from A");
}
}
class B extends A{
public void show(){
System.out.println("from B");
}
}
class C extends B{
public void show(){
System.out.println("from C");
}
}
class Main{
public static void main(String[] aa){
C ob=new C();
ob.show();
B ob1=new B();
ob1.show();
A ob2=new A();
ob2.show();
}
}
o/p : from C
from B
from A

*********** Software engineer at TATA CONSULTANCY SERVICES, mail to : [email protected], mob: 9681137876 ***********
5. Inheritance Shekhar 5

PROGRAM 5:

class A{
public int z=30;
}
class B extends A{
public int v=50;
}
class C extends B{
public int m=60;
}
class Main{
public static void main(String[] aa){
C ob=new C();
System.out.println(ob.z+ob.v+ob.m);
}
} o/p : 140

PROGRAM 6:

class A{
public void show(){
System.out.println("A");
}
}
class B{
public void show(){
System.out.println("B");
}
}
class C extends A,B{
public static void main(String[] aa){
C ob=new C();
ob.show();
}
}

NOTE:
Java does not support multiple inheritance. It creates ambiguity, and the name of this problem is Diamond
of death.

*********** Software engineer at TATA CONSULTANCY SERVICES, mail to : [email protected], mob: 9681137876 ***********

You might also like