Inheritance
Session - 2
1
Types of
Inheritance
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Multiple Inheritance
• Hybrid Inheritance
• On the basis of class, there can be three types of
inheritance: single, multilevel, and
hierarchical.
• Multiple inheritance is not supported in Java through
class.
• In java programming, multiple and hybrid
inheritance is supported through
interface only. 2
Types of
Inheritance
3
Single
When a classInheritance
inherits another class, it is known
as a single inheritance.
Class A
{
void methodA()
{
[Link]("Base class method");
}
}
Class B extends A
{
void methodB()
{
[Link]("Child class method");
}
public static void main(String args[])
{
B obj = new B();
[Link](); //calling super class method
[Link](); //calling local method 4
}
Single
Inheritance
class Animal{
void eat()
{[Link]("eating...");}
}
class Dog extends Animal{
void bark()
{[Link]("barking...");}
}
class DemoSingleInheritance{
public static void main(String
args[]){
Dog d=new Dog();
[Link]();
[Link]();
5
}
Multi level
When there Inheritance
is a chain of inheritance, it is
known as multilevel inheritance.
Class X Class Z extends Y
{ {
void methodX() void methodZ()
{ {
[Link]("Clas [Link]("class
s X method"); Z method");
} }
} public static void
Class Y extends X main(String args[])
{ {
void methodY() Z obj = new Z();
{ [Link](); //calling grand
[Link]("clas parent class method
s Y method"); }} [Link](); //calling
parent class method 6
Multi level
Inheritance
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void bark()
{[Link]("barking...");}
}
class BabyDog extends Dog{
void weep()
{[Link]("weeping...");}
}
class DemoMultilevelInheritance{
public static void main(String args[])
{
BabyDog d=new BabyDog();
[Link]();
7
[Link]();
Hierarchical
Inheritance
• When two or more classes inherits a
single class, it is known as hierarchical
inheritance.
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void bark(){[Link]("barking...");}
}
class Cat extends Animal{
void meow(){[Link]("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat(); [Link]();
[Link]();
8
//[Link]();//[Link]
Multiple
class A{
Inheritance Why multiple inheritance is not
supported in java?
• To reduce the complexity and
void msg() simplify the language, multiple
{[Link]("Hello inheritance is not supported in
java.
");} • Consider a scenario
where A, B, and C are
} three classes.
class B{ • The C class inherits A
void msg() and B classes.
• If A and B classes have
{[Link]("Welc the same method and
ome");} we call it from child
} class object, there will
be ambiguity to call the
class C extends A,B{
method of A or B class.
public static void • Since compile-time
main(String args[]){ errors are better than
runtime errors, Java
C obj=new C(); renders compile-time
9
error if we inherit 2
Practice
Create a classProgram
named 'Member' having the
following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 – Salary
printSalary()
It also has a method named 'printSalary' which
prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits
the 'Member' class. The 'Employee' and 'Manager'
classes have data members 'specialization' and
'department' respectively. Now, assign
10 name, 17
Class Diagram for Practice
+---------------------+
| Member |
Program
+---------------------+
| - name: String |
| - age: int |
| - phoneNumber:
String|
| - address: String |
| - salary: double |
+---------------------+
| + printSalary() |
+---------------------+ ^
^ |
| +---+---+
+---+---+ | |
| | +---------------------+
+---------------------+ | Manager |
| Employee | +---------------------+
+---------------------+ | - department: String |
| - specialization: String | +---------------------+
+---------------------+
11