Inheritance
Inheritance
Inheritance :
~~~~~~~~~~~
Creating a new class from the existing class is known as inheritance.
Usages :
1) Code reusability
2) Time saving
Types of inheritance :
~~~~~~~~~~~~~~~~
1) Single inheritance (JAVA)
2) multiple inheritance (x)
3) multilevel inheritance (JAVA)
4) hierarchical inheritance (JAVA)
5) hybrid inheritance. (x)
Single Inheritance :
-----------------------------
Only one parent , only one child.
Parent
|
Child
Multiple Inheritance :
---------------------------------
Multiple parent , only one child
Level1
|
Level2
|
Level3
.....
Hiearchical Inheritance :
~~~~~~~~~~~~~~~~~~~~~~~
Single parent, multiple child
Parent
|
---------------------------------------------------------
| | |
child1 child2 child3..
Hybrid Inheritance :
~~~~~~~~~~~~~~~~
Combination of other types.
class1
|
----------------------------------------------------------
| |
class2 class3
| |
----------------------------------------------------------
|
class4
Note :
JAVA will not support multiple parents. So, here we can't use multiple and hybrid inheritances. To
overcome this problem, we can use interfaces .
How to Do inheritance :
~~~~~~~~~~~~~~~~~~~~~~~~
extends -> is used to inherit
Syntax:
class parent
{
....
....
}
class child extends parent
{
......
......
}
void display()
{
System.out.println("\nemployee Name : " + ename);
System.out.println("employee no : " + empno);
System.out.println("Department:"+edept);
System.out.println("Salary:"+sal);
}
public static void main(String arg[])throws Exception
{
emp e1,e2,e3;
e1= new emp();
e2= new emp();
e3= new emp();
e1.read();
e2.read();
e3.read();
e1.display();
e2.display();
e3.display();
}
Child program:
class employee extends emp
{
float hra,ta,mda,pf,netsal;
void display2()
{
display();
hra= sal*0.10f;
ta= sal*0.12f;
mda= sal*0.08f;
pf= sal*0.14f;
netsal = sal+hra+mda+ta-pf;
System.out.println("HRA : " + hra);
System.out.println("TA : " + ta);
System.out.println("MDA : " + mda);
System.out.println("PF : " + pf);
System.out.println("NETSAL : " + netsal);
}
}
}
Hierarchical inheritance
========================
import java.util.*;
class person
{
String name,gender;
int age;
Scanner s= new Scanner(System.in);
void read()
{
System.out.println("Enter your name,gender and age : ");
name = s.next();
gender =s.next();
age = s.nextInt();
}
void display()
{
System.out.println("Name : " + name);
System.out.println("Gender : " + gender );
System.out.println("Age : " + age);
}
}
void read()
{
System.out.println("Student Details: ");
super.read();
System.out.println("Enter Course name, duration and fee :");
course=s.next();
duration=s.next();
fee =s.nextFloat();
}
void display()
{
super.display();
System.out.println("Course Details :\n\t" + course);
System.out.println("\t" + duration + "\n\t" + fee);
}
void showresult()
{
System.out.println("Student result is pass");
}
void display()
{
System.out.println("Patient Details :" );
super.display();
System.out.println("Problem : " + pbm);
System.out.println("Consulting Doctor: " + doc);
}
class mymain
{
public static void main(String arg[])
{
}
}