• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Inheritance

Extending the Properties of one class to another class is known as Inheritance.The main aim of the Inheritance is reusability.
In java language these inheritances are classified into following types.

1.Single or simple Inheritance
2.Multilevel Inheritance
3.Hierarchical Inheritance
4.Multiple Inheritance
5.Hybrid Inheritance



Single Inheritance
Extending the properties of single parent class to single child class is known as Single or simple Inheritance.
Syntax:

class parent
{
..
..
}
class child extends parent
{
..
..
}


Note: Here "extends" Keyword is used to extends the parent class properties to child class.

Program for Student Details using Single Inhiritance concept
import java.util.*;
class A
{
String name,location,address;
char gender;
}

class Student extends A
{
String id;
float marks,fee;
Student()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter marks");
marks=s.nextFloat();
System.out.println("Enter fee");
fee=s.nextFloat();
}
void studentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("Student id is "+id);
System.out.println("Marks of the student is "+marks);
System.out.println("Student Fee is "+fee);
}
}
class Main
{
public static void main(String args[])
{
Student st=new Student();
st.studentDetails();
}
}


Output:
Enter name
sateesh
Enter location
Hyd
Enter address
kukatpally
Enter gender
M
Enter id
1224
Enter marks
98
Enter fee
1000
Name of the student is sateesh
location of the student is Hyd
Address of the student is kukatpally
Gender of the student is M
Student id is 1224
Marks of the student is 98.0
Student Fee is 1000.0
MultiLevel Inheritance
Extending the propertis of one class to another class in a Sequential order is known as Multilevel Inheritance.
Syntax:

class A
{
..
..
}
class B extends A
{
..
..
}
class C extends B
{
..
..
}
class D extends C
{
..
..
}


Note:if we create object for the 'D' class we can access A,B,C,D Classes.

Program for Student Details using multilevel Inhiritance concept

import java.util.*;
class A
{
String name,location,address;
char gender;
}
class B extends A
{
String id;
}
class Faculty extends B
{
float salary;
Faculty()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter Salary");
salary=s.nextFloat();
}
void facultyDetails()
{
System.out.println("Name of the Faculty is "+name);
System.out.println("location of the Faculty is "+location);
System.out.println("Address of the Faculty is "+address);
System.out.println("Gender of the Faculty is "+gender);
System.out.println("Faculty id is "+id);
System.out.println("Faculty Salary is "+salary);
}
}
class Main
{
public static void main(String args[])
{
Faculty F=new Faculty();
F.facultyDetails();
}
}


Output:
Enter name
Sateesh Enter location
Delhi
Enter address
del
Enter gender
m
Enter id
23
Enter Salary
20000
Name of the Faculty is Sateesh
location of the Faculty is Delhi
Address of the Faculty is del
Gender of the Faculty is m
Faculty id is 24
Faculty Salary is 24000.0
Hierarchical Inheritance
Extending the propertis of Single Parent class to multiple child classes known as Hierarchical Inheritance.
Syntax:

class A
{
..
..
}
class B extends A
{
..
..
}
class C extends A
{
..
..
}
class D extends A
{
..
..
}

Program for Student Details using Hirarchy Inhiritance concept

import java.util.*;
class A
{
String name,location,address;
char gender;
}
class Student extends A
{
String sid;
float marks,fee;
Student()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next(); System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter sid");
sid=s.next();
System.out.println("Enter marks");
marks=s.nextFloat();
System.out.println("Enter fee");
fee=s.nextFloat();
}
void studentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("Student id is "+sid);
System.out.println("Marks of the student is "+marks);
System.out.println("Student Fee is "+fee);
}
}
class Faculty extends A
{
String fid;
float salary;
Faculty()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter fid");
fid=s.next();
System.out.println("Enter Salary");
salary=s.nextFloat();
}
void facultyDetails()
{
System.out.println("Name of the Faculty is "+name);
System.out.println("location of the Faculty is "+location);
System.out.println("Address of the Faculty is "+address);
System.out.println("Gender of the Faculty is "+gender);
System.out.println("Faculty id is "+fid);
System.out.println("Faculty Salary is "+salary);
}
}
class Main
{
public static void main(String args[])
{
Student st=new Student();
st.studentDetails();
Faculty F=new Faculty();
F.facultyDetails();
}
}


Output:
Enter name
sateesh Enter location
hyd
Enter address
kukatpally
Enter gender
m
Enter sid
1224
Enter marks
24
Enter fee
2400
Name of the student is sateesh
location of the student is hyd
Address of the student is kukatpally
Gender of the student is m
Student id is 1224
Marks of the student is 24.0
Student Fee is 2400.0

Enter name
Tryelif
Enter location
Germany
Enter address
germany
Enter gender
f
Enter fid
24
Enter Salary
24000
Name of the Faculty is Tryelif
location of the Faculty is Germany
Address of the Faculty is germany
Gender of the Faculty is f
Faculty id is 24
Faculty Salary is 24000.0

Multiple Inheritance


Extending the propertis of Multiple parent classes to single child class known as Multiple Inheritance
Syntax:

class A
{
..
..
}
class B
{
..
..
}
class C
{
..
..
}
class D extends A,B,C
{
..
..
}


Note: In Java Language Multiple Inheritance con't be supported at class level .To overcome this problem Interface is introduced.



Hybrid Inheritance
Combination of above all Inheritances is known as Hybrid Inheritance
Syntax:

class A
{
..
..
}
class B
{
..
..
}
class C extends A
{
..
..
}
class D extends B
{
..
..
}