1.
Single inheritance:
If we are getting variable and methods from
only one super class to one more number sub classes, then it is called single
inheritance.
2.
Multiple:
If we get variable and method from more
then one super class to one or more number of sub classes then it is called
multiple inheritance.
On the basis of single and multiple inheritances there are 3
more inheritances.
1.
Multilevel inheritance
2.
Hierarchical inheritance
3.
Hybrid inheritance.
1.
Multilevel inheritance:-
It is a combination of single inheritance
in more than one level.
2.
Hierarchical inheritance :-
It is a combination of single inheritance in
a particular structure like Binary tree.
3.
Hybrid inheritance :
It is the combination of single and
multiple inheritances.
Java is not support this inheritance.
Ex code:-
class Employee
{
String
eid;
String
ename;
String
eaddr;
public
void getEmpDetails()
{
System.out.println("Employee
Id :"+eid);
System.out.println("Employee
Name :"+ename);
System.out.println("Employee
Address:"+eaddr);
}
}
class Manager extends Employee
{
Manager(String
eid1, String ename1, String eaddr1)
{
eid=eid1;
ename=ename1;
eaddr=eaddr1;
}
public
void getManagerDetails()
{
System.out.println("Manager
Details");
System.out.println("---------------");
getEmpDetails();
}
}
class Accountant extends Employee
{
Accountant(String
eid1, String ename1, String eaddr1)
{
eid=eid1;
ename=ename1;
eaddr=eaddr1;
}
public
void getAccountantDetails()
{
System.out.println("Accountant
Details");
System.out.println("------------------");
getEmpDetails();
}
}
class Test
{
public
static void main(String[] args)
{
Manager
m=new Manager("E-111","AAA","Hyd");
m.getManagerDetails();
System.out.println();
Accountant
acc=new Accountant("E-222","BBB","Hyd");
acc.getAccountantDetails();
}
}
Output:-
Manager Details
---------------
Employee Id :E-111
Employee Name :AAA
Employee Address:Hyd
Accountant Details
------------------
Employee Id :E-222
Employee Name :BBB
Employee Address:Hyd





No comments:
Post a Comment