Inheritance in Java
Post-Intro:-In this post we will learn what is Inheritance why is
important where is used and how to work in java with an example so let's go
start now.
Table Of Content:
Java inheritance in Hindi :-
जैसे पिता की संपत्ति पर पुत्र का अधिकार होता है और पुत्र उस संपति का उपयोग
करता है। वैसे ही यदि आप चाहे तो एक class को पिता (super class) बना सकते
है, और दूसरी class को पुत्र (sub class) बना सकते है। ऐसा करने से एक class
class properties (methods, variables आदि) को access कर सकती है। ऐसा करने
से पुत्र (sub class) को पिता (super class) कि सम्पूर्ण property (methods,
variable आदि) को access करने के अधिकार प्राप्त हो जाते है।
कल्पना कीजिये आपने कोई class पहले से create की हुई है और इस class में कुछ
ऐसे methods है जो आपकी किसी दूसरी class में भी काम आ सकते है। इन methods
को दुबारा लिखने की बजाय आप पिछली पुरानी class में सं उन methods को access
कर सकते है। ऐसा कर के लिए जो class methods को access करना चाहती है उसे
दूसरी class की sub class बनना होगा। Sub class बनने के लिए आपकी class को उस
दूसरी class को extend करना होगा। इसे ही inheritance कहते है।
Inheritance से आप एक ही code को बार बार लिखने की उलझन से बच जाते है।
Inheritance की इस खूबी को re-usability कहते है। यानि एव ही code को बार बार
अलग अलग जगह पर use किया जा सकता है।
जब कोई एक class दूसरी class को inherit करन चाहती है तो वह extends keyword
यूज़ करती है। और यदि आप चाहते है की आपके के द्वारा बनायीं हुई class को कोई
दूसरी class inherit ना करे तो इसके लिए आप class के नाम से पहले final
keyword लगा देते है। जिन classes के नाम से पहले final keyword होता है
उन्हें inherit नहीं किया जा सकता है।
कोई भी class सिर्फ एक ही class को extend कर सकती है। जब कोई class एक से
अधिक classes को extend करती है तो वह multiple inheritance allow नही
है, इसकी जगह पर multiple inheritance use कर सकते हैं।
जिस class को inherit किया जाता है वह super class कहलाती है। और जो class
inherit करती है वह sub class कहलाती है। यँहा पर एक ध्यान देने योग्य बात ये
है, की sub class- super class के सभी methods और variables को access नहीं
कर सकती है। जो methods public और protected declare किये हुए है उन्हें ही
sub class access कर सकती है | Super class के किसी भी private member को sub
class access नहीं कर सकती है। यदि super class ने किसी और class को extend
कर रखा है तो उस class के भी सभी public और protected members को आपकी class
यूज़ कर सकती है।
Java inheritance in English:-
- The Process of getting the property of one class into another class is called Inheritance.
- In other words we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or subclass and the old class is called Base or Parent or Superclass.
- When a class inherits a class's property, it can access all the data member and member functions of that class except the private element.
- In this type of programming mainly two types of classes are used.
Inheritance Class:-
- Parent/Super/Base class:- The class which is inherited by another class is called Parent or Super or Base class.
- Child/Sub/Derived class:- The class which is inherit the another class is called Child or Sub or Derived Class.
How to Inherit One Class into another
यदि आप किसी class को inherit करना चाहते है तो आप extends कीवर्ड उसे करते है। आप अपनी class के नाम के बाद extends keyword लगाते है और उसके बाद आप जिस class को inherit करना चाहते हैं, उस class को नाम लिखते हैं so let se an demo for better understanding.
**** Derived class extends Base class *******
Explanation: Here B is a Derived class and A is a Base class and extends is a keyword which is used to inherit one class into another, so let's see an example:-
*******Example :*******
//How to Inhrit let's see an example
class A { // Parent Class
void base(){
System.out.println("I am Parent Class");
}
}
class B extends A{ // Child class
void derived(){
System.out.println("I am Child Class");
}
}
class Demo{
public static void main(String arg[]){
B obj= new B(); // create object
obj.base(); //calling Base Class Object
obj.derived(); //calling Child Class Object
}
}
Explanation:- Here class A is a base class and B is a derived class
because A is inherited into B therefore we can call all the functions
using object of B.
********OUTPUT*****
------------------java Demo-----------------
I am Parent class
I am Child Class
Advantage of Inheritance:
- Code Reusability: It means function inside base class is shared by all the derived class.
- Time Saving: Because there is no need to define existing property(same code) of a class in another class.
- Less Cost: Because existing code is reused, it leads to less development and maintenance costs.
- It helps to reduce code redundancy.
Types of Inheritance:
- There are five types of Inheritance but in case of java basically three types:-
- (Java में 3 तरह से inheritance को यूज़ किया जाता है। आपकी application के लिए आपको जो suitable लगे आप वही तरीका यूज़ कर सक है। इन तीनों तरीकों के बारे में निचे दिया जा रहा है।)
- 1 Single Inheritance
- 2.Multilevel Inheritance
- 3.Herarchical Inheritance
But in this post we are discuss on Complete Five types of Inheritance. so
let begin one by one example.
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
SINGLE INHERITANCE
- In Hindi:- Single inheritance में एक class किसी दूसरी क्लास को extend करती है। इस तरह के के inheritance का उपयोग basic programming में किया जाता है।
- In English :- In this types of inheritance only two classes are used in which one is inherited by another.
*******Example :*******
//1. Single Inheritance
class Add{ // Base Class
void add(){
int a=10,b=20;
int c=a+b;
System.out.println("Addition Of A And B:"+c);
}
}
// Now Create Derived inherit Base Class
class Sub extends Add{ // Derived Class
void sub(){
int a=100,b=50;
int c= a-b;
System.out.println("Subtraction Value of A and B:"+c);
}
}
class DemoSingle{
public static void main (String arg[]){
Sub obj= new Sub(); // createring object
obj.add(); // calling base class funtion
obj.sub(); // calling derived class funtion
}
}
Explanation :- In the above example we can see that two class are used
where Addition is base class and Subtraction is Derived class then
Subtraction inherited the all property of base class(Addition ) so this
type of work single inheritance.
********OUTPUT*****
PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java DemoSingle
Addition Of A And B:30
Subtraction
Value of A and B:50
MULTIPLE INHERITANCE
- When two or more than two classes are inherited by a single class simultaneously called multiple inheritance.
- In other word we can say that in this type of inheritance Base class may be two or more than two but derived class should be one.
- In this type of inheritance at least three class are compulsory.
- Java does not support multiple inheritance therefor interface are used to implement multiple inheritance.
- interface is declared with interface keyword and it is implemented by a class while class is extended by a class.
- we can not define function inside an interface ,only can be declared.
- It is the responsibility of derive class to implement/define the method of interface.
*******Example :*******
2. Mutliple Inhritance
interface Addition{ //declaring method because
// we can not define function inside interface
public void add();
}
class Substraction{
public void sub(){
int a=200, b=10;
int c= a-b;
System.out.println("Substraction of a and b:"+c);
}
}
// Derived class extending base class and implementing interface
class Multiplication extends Substraction implements Addition{
public void add(){ // implementing methods of interface
int a=100, b= 200;
int c= a+b;
System.out.println("Addition of a and b:"+c);
}
void mul(){
int a=5, b=4;
int c=a*b;
System.out.println("Multiplication of a and b:"+c);
}
}
class DemoMiltiple{
public static void main(String arg[]){
Multiplication obj= new Multiplication();
obj.mul();
obj.sub();
obj.add();
}
}
Explanation:- In the above example you can see that there
are two classes(Subtraction and Multiplication) and one interface
(Addition) are used in which Subtraction is extended and Addition is
implemented by Multiplication therefore using object of Multiplication
we can call function add() , sub() and multiply().
********OUTPUT*****
Multiplication of a and b:20
Subtraction of a and b:190
Addition of a and b:300
MULTILEVEL INHERITANCE
- Multilevel inheritance में एक class दूसरी क्लास को extend करती है और दूसरी class तीसरी class को extend करती है एंड so on चलते रहते है।
- When first class is inherited by second class, second class is inherited by third class and so on called multilevel inheritance.
- In this type of inheritance each derived class is the base class for the next class.
- In this type of inheritance at least three class are compulsory.
*******Example *******
/
class Addition{
void add(){
int a=19, b=11;
int c=a+b;
System.out.println("Addition of A AND B:-"+c);
}
}
class Substraction extends Addition{
void sub(){
int a=19, b=10;
int c=a-b;
System.out.println("Substraction of A AND B:-"+c);
}
}
class Multiplication extends Substraction{
void mul(){
int a=5, b=10;
int c=a*b;
System.out.println("Multiplication of A AND B:-"+c);
}
}
class DemoMiltilevel{
public static void main(String arg[]){
Multiplication obj=new Multiplication();
obj.mul();
obj.sub();
obj.add();
}
}
******* OUTPUT *******
Multiplication of A AND B:-50
Subtraction of A
AND
B:-9
Addition of A AND B:-30
HERARCHICAL INHERITANCE
- Hierarchical inheritance में एक class को बहुत सी classes extend करती है। इस तरह inheritance का प्रयोग tab किया जाता जब super class के task के कई sub task होते है।
- When a single class is inherited by two or more than two classes simultaneously called it hierarchical inheritance.
- In other word we can say that in this type of inheritance derived class may be two or more than two but Base class should be one.
- In this type of inheritance at least three class are compulsory.
*******Example *******
// Hierarchical Inheritance
ition{
void add(){
int a=100,b=300;
int c=a+b;
System.out.println("Addition of a and b is :"+c);
}
}
class Subtraction extends Addition{ //extending Addition
void sub(){
int a= 220, b=20;
int c=a-b;
System.out.println("Subtraction of a and b :- "+c);
}
}
class Multiplication extends Subtraction{ //extending Subtraction
void mul(){
int a=2,b=10;
int x=a*b;
System.out.println("Multiliplication a and b is: "+x);
}
}
class DemoHierarchical{
public static void main(String arg[]){
Multiplication obj= new Multiplication();
obj.mul();
obj.sub();
obj.add();
}
}
Explanation :- In the above example you can see that there are three
classes(Addition, Subtraction and Multiplication) are used in which Addition
is inherited by Subtraction and Multiplication therefore using object of
Multiplication we can call function only add() and multiply() because there
is no relation between Subtraction and Multiplication therefore function
sub() can not be called by object of Multiplication. Similarly by using
object of class Subtraction we can call only function add() and sub().
******* OUTPUT *******
Multiplication a and b is:
20
Subtraction of a and b : 200
Addition of a and b is : 400
HYBRID INHERITANCE
- The combination of two or more than two inheritance is called Hybrid inheritance.
- It can be combination of any two or more than two inheritance(single, multiple, multilevel, hierarchical).
- In this type of inheritance at least three class are compulsory.
*******Example *******
// Hybrid Inheritance:
interface Addition
{
//declaring method
//because we can not define function inside interface
void add();
}
//Derived class
class Subtraction{
void sub()
{
int x,y=30,z=10;
x=y-z;
System.out.println("Subtraction="+x);
}
}
//Derived class extending base class and implementing interface
class Multiplication extends Subtraction implements Addition{
//implementing method of interface
public void add()
{
int x,y=30,z=10;
x=y+z;
System.out.println("Addition ="+x);
}
void mul()
{
int x,y=30,z=10;
x=y*z;
System.out.println("Multiplication="+x);
}
}
class Division extends Multiplication{
void div()
{
int x,y=30,z=10;
x=y/z;
System.out.println("Division="+x);
}
}
class DemoHybrid{
public static void main(String[] arg)
{
Division obj=new Division();
obj.add();
obj.sub();
obj.mul();
obj.div();
}
}
Explanation: In the above example you can see that there are four
classes (Addition, Subtraction, Multiplication and Division) in which
Addition and Subtraction are inherited by Multiplication class so in class
Addition, Subtraction and Multiplication there is Multiple inheritance but
class Multiplication is inherited by Division so in class Multiplication and
Division there is Single inheritance. Therefore the above program is a
combination of Multiple and Single inheritance so it is called Hybrid
Inheritance.
******* OUTPUT *******
Addition =40
Subtraction=20
Multiplication=300
Division=3
This article is contributed by Shailesh. Please write comments if you find
anything incorrect, or you want to share more information about the topic
discussed above.
Comments