In this article,Inheritance in Java is explained in detail with examples.
Acquiring the properties and behaviours of an existing class into a new class is known as "inheritance in Java".
Acquiring the properties and behaviours of an existing class into a new class is known as "inheritance in Java".
Ex
This relationship of inheritance is known as "Kind of relationship".
i.e, "class-B is a kind of class-A".
Advantage of inheritance is
- Reusability
- Minimal code Redundancy
How to perform Inheritance?
"Extends" is the keyword using which we perform inheritance. (From topic Inheritance in Java)
Syntax
class A
{
----
}
class B extends A
{
-----
}
Ex
class A
{
int x,y;
void m1()
{----}
}
class B extends A
{
int a,b;
void m2() // here x,y; m1(),{--} are Base class.This base class is inherited into the subclass.
{----}
}
Ex
//program related to Inheritance in Java
class Add
{
int n1,n2;
int addnumber()
{
int sum=0;
sum=n1+n2;
return sum;
}
}
class Sub extends Add
{
int subnumber()
{
int sub;
sub=n1-n2;
return sub;
}
}
class InheritacneDemo
{
public static void main(String args[])
{
Sub x=new Sub();
x.n1=10;
x.n2=4;
int a=x.addnumber();
int b=x.subnumber();
System.out.println(x.n1);
System.out.println(x.n2);
System.out.println(a);
System.out.println(b);
}
}
Output
Types of Inheritance in Java.
Based on the number of base classes and its derivations,Inheritance in Java are divided into 4-types.
- Single Inheritance.
- Multi-Level Inheritance.
- Hierarchical Inheritance.
- Hybrid Inheritance.
In this type, a single base class gives derivation to a single sub-class.
Ex
//program related to Inheritance in Java--single inheritance.
class Room
{
int len,bre;
Room(int x, int y)
{
len=x;
bre=y;
}
int area()
{
return(len*bre);
}
}
class StudyRoom extends Room
{
int height;
StudyRoom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return len*bre*height;
}
}
class SingleInheritance
{
public static void main(String args[])
{
StudyRoom r=new StudyRoom(14,12,10);
int area1=r.area();
int volume1=r.volume();
System.out.println("Area is: "+area1);
System.out.println("Volume is: "+volume1);
}
}
Output
Multi-level inheritance.
This inheritance is an extension of single-inheritance. Here, sub-class acts as a bass-class for further derivations. (From topic Inheritance in Java)
Ex
//program related to Inheritance in Java--Multi-level inheritance
class A
{
int x,y;
A(int p, int q)
{
x=p;
y=q;
}
void displayA()
{
System.out.println("X: "+x); //(From topic Inheritance in Java)
System.out.println("Y: "+y);
}
}
class B extends A
{
int a;
B(int r,int s,int t)
{
super(r,s);
a=t;
}
void displayB()
{
System.out.println("A: "+a);
}
}
class C extends B
{
int d,e;
C(int i,int j,int k,int l,int m)
{
super(i,j,k);
d=l;
e=m;
}
void displayC()
{
System.out.println("D: "+d);
System.out.println("E: "+e);
}
}
class MultiLevelInheritance
{
public static void main(String args[])
{
C obj=new C(1,2,3,4,5);
obj.displayA();
obj.displayB();
obj.displayC();
}
}
Output
Hierarchical Inheritance
In this type of Inheritance,we have a single base class and more than one subclass. ( From topic Inheritance in Java)
Ex
//Program related to Inheritance in Java--hierarchical inheritance.
class A{
int a,b;
A(int x,int y)
{
a=x;b=y;
}
void displayA()
{
System.out.println("A: "+a);
System.out.println("B: "+b);
}
}
class B extends A
{
int c;
B(int p,int q,int r)
{
super(p,q);
c=r;
}
void displayB()
{
System.out.println("C: "+c);
}
void displayA()
{
System.out.println("A: "+a);
System.out.println("B: "+b);
System.out.println("C: "+c);
}
}
class C extends A
{
int d;
C(int l,int m,int n)
{
super(l,m);
d=n;
}
void displayC()
{
System.out.println("D: "+d);
}
void displayA()
{
System.out.println("A: "+a);
System.out.println("B: "+b);
System.out.println("D: "+d);
}
}
class HierarchicalInheritance
{
public static void main(String args[])
{
System.out.println("Using B object");
B obj=new B(1,2,3);
obj.displayA();
obj.displayB();
System.out.println("Using C Object");
C obj1=new C(4,5,6);
obj1.displayA();
obj1.displayC();
System.out.println("Hello world");
A a; //object-reference-variable.
a=new B(10,20,30);
a.displayA();
//a.displayB(); //it is subclass own method.
a=new C(11,22,33);
a.displayA();
//a.displayC(); // It is sub class own method
}
}
Output
Hybrid Inheritance.
This inheritance is a combination of above 3 types of inheritance.
Imp Note.
Java doesn't support multiple inheritances, two or more base classes giving derivation to a single sub-class.
Reason
A special case of hybrid inheritance, in which we have hierarchical inheritance at the top and multiple inheritance at the bottom leads to ambiguity problems.This case is known as "Multi path inheritance".
(From topic Inheritance in Java)
(From topic Inheritance in Java)
Special case (Hybrid Inheritance)
- In this case,properties of class A(x,y) are inherited to class-D, in the path A-B-D and A-C-D, this leads to confusion,ambiguity in class D of inheriting properties of class-A twice.
- Hence this special case of Hybrid Inheritance i.e, Multi-path inheritance is due to multiple Inheritance, that is the reason why java does not support multiple inheritances through classes
- However,in java multiple inheritances is done through interfaces.
- In C++,this concept is supported by "Virtual base classes". (From topic Inheritance in Java)
Continue to the next topic Dynamic Method Dispatch.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment