Contact Form

Name

Email *

Message *

Search This Blog

Image

Implementation of Inheritance in JAVA




Aim:-Implementation of Inheritance in JAVA

In this post I am going to show you “Implementation of Inheritance in JAVA. The real time example of inheritance is parents and children. The children Inherits the habits of parents. Like this in JAVA we use inheritance to reuse the feature of the base class in the derived class.

Inheritance is main feature Object-Oriented Program(OOP) or java. It is also a example of reuse ability of OOP paradigm. JAVA supports the inheritance. In JAVA there is mainly of four types of Inheritance: 1) Single Inheritance 2) Hierarchical Inheritance 3) Multilevel Inheritance 4) Multiple Inheritance.


Types of Inheritance

1. Single Inheritance:-

In Single Inheritance there is only one base class and one derived class or in other words there is only one parent class and one child class. Here is example of the Single Inheritance:

Code:-
//Derived Class or Parent Classclass Room{	int length;	int breadth;	Room(int x, int y){		length=x;		breadth=y;	}	int area(){		return(length*breadth);	}}//Base Class or Child Classclass BedRoom extends Room{					//Inherting the Room Class	int height;	BedRoom(int x,int y,int h){		super(x,y);							//Calling the Base Class Constructor		height = h;	}	int volume(){		return(length*breadth*height);	}}//Main Classclass InherTest{	public static void main(String args[]){		BedRoom room1=new BedRoom(10,20,30);		int area1= room1.area();			//Dervied Class Method		int volume1=room1.volume();			//Base Class Method		System.out.println("Area = "+area1);		System.out.println("volume = "+volume1);	}}


2. Hierarchical Inheritance:-

In Hierarchical Inheritance there are two or more base classes and one derived class or in other words there is only one parent class and two or more child classes. Here is example of the Hierarchical Inheritance:

Code:-
//Derived Class or Parent Classclass Room{	int length;	int breadth;	Room(int x, int y){		length=x;		breadth=y;	}	int area(){		System.out.println("In Room Class");		return(length*breadth);	}}//Base Class or Child Classclass BedRoom extends Room{					//Inherting the Room Class	int height;	BedRoom(int x,int y,int h){		super(x,y);							//Calling the Base Class Constructor		height = h;	}	int volume(){		System.out.println("In Bed Room Class");		return(length*breadth*height);	}}//Base Class or Child Classclass DrawingRoom extends Room{					//Inherting the Room Class	int height;	DrawingRoom(int x,int y,int h){		super(x,y);							//Calling the Base Class Constructor		height = h;	}	int volume(){		System.out.println("In Drawing Room Class");		return(length*breadth*height);	}}//Base Class or Child Classclass WashRoom extends Room{					//Inherting the Room Class	int height;	WashRoom(int x,int y,int h){		super(x,y);							//Calling the Base Class Constructor		height = h;	}	int volume(){		System.out.println("In Wash Room Class");		return(length*breadth*height);	}}//Main Classclass HerarichicalInheritanceTest{	public static void main(String args[]){		/************BedRoom**********************/		BedRoom room1=new BedRoom(10,20,30);		int area1= room1.area();			//Dervied Class Method		int volume1=room1.volume();			//Base Class Method		System.out.println("Area and Volume of Bed Room");		System.out.println("Area = "+area1);		System.out.println("volume = "+volume1);		/************DrawingRoom**********************/		DrawingRoom room2=new DrawingRoom(20,30,30);		area1= room2.area();			//Dervied Class Method		volume1=room2.volume();			//Base Class Method		System.out.println("Area and Volume of Drawing Room");		System.out.println("Area = "+area1);		System.out.println("volume = "+volume1);		/************WashRoom**********************/		WashRoom room3=new WashRoom(10,10,30);		area1= room3.area();			//Dervied Class Method		volume1=room3.volume();			//Base Class Method		System.out.println("Area and Volume of Wash Room");		System.out.println("Area = "+area1);		System.out.println("volume = "+volume1);	}}



3. Multilevel Inheritance:-

In multilevel Inheritance two or more level of inheritance done or in other words the base class also derived class.

Code:-
//Super Class or Parent Classclass Home{	int length;	int breadth;	Home(int x, int y){		length=x;		breadth=y;	}	int area(){		return(length*breadth);	}}//Intermidate super Classclass Room extends Home{					//Inherting the Home Class	int height;	Room(int x,int y,int h){		super(x,y);							//Calling the Base Class Constructor		height = h;	}	int volume(){		return(length*breadth*height);	}}//Subclassclass BedRoom extends Room{					//Inherting the Room Class	BedRoom(int x,int y,int h){		super(x,y,h);							//Calling the Base Class Constructor	}	void type(){		System.out.println("Sleeping");	}}//Main Classclass InherTest{	public static void main(String args[]){		BedRoom room1=new BedRoom(10,20,30);		int area1= room1.area();			//Dervied Class Method		int volume1=room1.volume();			//Base Class Method		System.out.println("Area = "+area1);		System.out.println("volume = "+volume1);		room1.type();	}}



4. Multiple Inheritance:-

JAVA does not supports the multiple inheritance, but we can implements multiple inheritance by using the concept of interfaces. we extends only one class after that we can implements multiple interfaces.

Code:-
class Student{	int rollnumber;	void getNumber(int n){		rollnumber=n;	}	void putNumber(){		System.out.println("Roll Number :"+rollnumber);	}}class Test extends Student{	float part1,part2;	void getMarks(float m1,float m2){		part1=m1;		part2=m2;	}	void putMarks(){		System.out.println("Marks Obtained");		System.out.println("Part 1 = "+part1);		System.out.println("Part 2 = "+part2);	}}interface Sports{	float sportwt = 6.0F;	void putWT();}class Results extends Test implements Sports{	float total;	public void putWT(){		System.out.println("Sports WT = "+sportwt);	}	void display(){		total=part1+part2+sportwt;		putNumber();		putMarks();		putWT();		System.out.println("Total Score = "+total);	}}class MultipleInheritence{	public static void main(String args[]){		Results student1 =new Results();		student1.getNumber(4321);		student1.getMarks(29.5F, 30.5F);		student1.display();	}}


Comments