Inheritance in Java

What is Inheritance

जब एक class किसी दूसरे class के गुण (data members) और कार्यक्षमताओ (methods) को ग्रहण करता है तो इस प्रक्रिया को inheritance कहते है| inheritance का उद्देश्य code का एक से अधिक बार उपयोग (code reusability) प्रदान करना है, ताकि एक class में केवल मुख्य विशेषताओ को लिखना पड़े बाकि सामान्य गुणों और कार्यक्षमताओ को दूसरे वर्ग से प्राप्त कर सके|

Child class

वह class जो किसी अन्य class की विशेषताओ को ग्रहण करता है उसे child class, sub class या derived class कहते है|

Parent class

जिस class के गुणों और क्रियाकलापों (functions) का उपयोग किसी अन्य class द्वारा किया जाता है, उसे parent class, super class या base class के नाम से जाना जाता है|

Inheritance अपने सामान्य डेटा सदस्यों (data members) और विधियों (methods) का विस्तार करके एक मौजूदा class के आधार पर एक नए class को परिभाषित करने की एक विधि है । Inheritance हमें कोड का पुन: उपयोग (code reusability) करने की अनुमति देता है|

नोट: inheritance का सबसे बड़ा फायदा यह है कि जो कोड पहले से ही base class में मौजूद है, उसे child class में दोबारा लिखने की जरूरत नहीं होती| इसका अर्थ है कि parent class के data members और methods का उपयोग child class में किया जा सकता है।


किसी class को inherit करने के लिए हम extend keyword का उपयोग करते है|

यहाँ XYZ, child class है और class, ABC parent class है, यहाँ class XYZ, class ABC के गुणों और methods को inherit कर रहा है|

class XYZ extends ABC
{
}

इस उदाहरण में, हमारे पास एक base class teacher और एक child class PhysicsTeacher है, यहाँ class PhysicsTeacher base class से designation, collage properties और work() method को inherit कर रहा है, इसलिए हमें इन properties को sub-class में declare करने की आवश्यकता नहीं है।

यहां हमारे पास designation, college name और work() method है जो सभी teachers के लिए सामान्य है इसलिए हमने उन्हें base class में declare किया है, इस तरह से MathTeacher, MusicTeacher और PhysicsTeacher जैसे child classess में इस कोड को लिखने की आवश्यकता नहीं है|

Example of Inheritance

class Teacher {
String designation = “Teacher”;
String collegeName = “ComputerHindiNotes”;
void does(){
System.out.println(“Teaching”);
}
}


public class PhysicsTeacher extends Teacher{
String mainSubject = “Physics”;
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}

परिणाम (Output):
ComputerHindiNotes
Teacher
Physics
Teaching

उपरोक्त उदाहरण के आधार पर हम कह सकते हैं कि PhysicsTeacher IS-A Teacher । इसका मतलब यह है कि एक child class का parent class के साथ IS-A relation है। यह inheritance child और parent class के बीच IS-A relation के रूप में जाना जाता है|

ध्यान दे

derived class उन सभी members और methods को inherit करता है जिन्हें public या protected घोषित किया जाता है। यदि super class के members या methods को private घोषित किया जाता है, तो derived class उन्हें सीधे उपयोग नहीं कर सकता है। private members को केवल अपनी ही class में access किया जा सकता है। ऐसे private members को केवल public या protected getter और super class के setter methods का उपयोग करके access किया जा सकता है जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है।

class Teacher {
private String designation = “Teacher”;
private String collegeName = “ComputerHindiNotes”;
public String getDesignation() {
return designation;
}
protected void setDesignation(String designation) {
this.designation = designation;
}
protected String getCollegeName() {
return collegeName;
}
protected void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
void does(){
System.out.println(“Teaching”);
}
}

public class JavaExample extends Teacher{
String mainSubject = “Physics”;
public static void main(String args[]){
JavaExample obj = new JavaExample();
/* Note: we are not accessing the data members
* directly we are using public getter method
* to access the private members of parent class
*/
System.out.println(obj.getCollegeName());
System.out.println(obj.getDesignation());
System.out.println(obj.mainSubject);
obj.does();
}
}
परिणाम (output):
ComputerHindiNotes
Teacher
Physics
Teaching

उपरोक्त उदाहरण में ध्यान देने योग्य महत्वपूर्ण बात यह है कि child class parent class के protected methods के माध्यम से parent class के private members को access करने में सक्षम है। जब हम एक instance variable (data members ) या method को protect करते हैं, तो इसका मतलब है कि वे केवल उस class में और child class में ही access किये जा सकते हैं। ये public, private और protected access specifier हैं|

Inheritance के प्रकार (Types of Inheritance)

  1. Single Inheritance : यह एक child और parent class के बीच के relation को प्रदर्शित करता है जहां एक class दूसरे class को extend करता है।
  2. Multilevel inheritance : यह एक child और parent class के relation को संदर्भित करता है जहां एक class child class का विस्तार करता है। उदाहरण के लिए class C class B को extend करेगा तो class B class A को extend करेगा|
  3. Multiple inheritance: जब एक class एक से अधिक classes का विस्तार करता है, तो इस प्रकार का inheritance multiple inheritance कहलाता है इस inheritance में एक child class के 2 या उससे अधिक parent class होते है|
  4. Hierarchical inheritance:- : यह एक से अधिक child classes और एक parent class के relation को संदर्भित करता है जहां एक से अधिक sub classes एक super class का विस्तार करती हैं। उदाहरण के लिए, classes B, C & D एक ही class A का विस्तार कर रही है|
  5. Hybrid inheritance:- एक program में जब एक से अधिक inheritances का संयोजन होता है तो इस प्रकार के inheritance को hybrid inheritance कहा जाता है। उदाहरण के लिए class A और B class C का विस्तार करती है और अन्य class D class A का विस्तार करती है तो यह एक hybrid inheritance का उदाहरण है क्योंकि यह single और hierarchical inheritance का संयोजन है।

महत्वपूर्ण बिंदु

जावा में Multiple inheritance और Hybrid inheritance क्लास के जरिये नहीं बनाया जा सकता है|

जावा में Multiple inheritance क्यूँ नहीं होता है?
Why multiple inheritance is not supported in java?

जटिलता को कम करने और प्रोग्रामिंग को सरल बनाने के लिए, जावा में multiple inheritance नहीं होता है।

उदाहरण के लिए मान लीजिये की A, B और C तीन class हैं, C क्लास में A और B क्लास को inherit किया गया है और A और B क्लास में एक जैसे function बने हुए हैं| अब यदि C क्लास के ऑब्जेक्ट के जरिये इनमे से किसी function को कॉल (Call) किया गया तो कम्पाइलर को कैसे पता चलेगा की किस क्लास के function को call करना है (Class A के function को या Class B के function को)| चूँकि compile-time error runtime-error से बेहतर होती है, इसलिए जावा में multiple inheritance प्रयोग करते समय compile-time error आती है|

अब हम उदाहरणों के माध्यम से प्रत्येक inheritance को एक-एक करके समझेंगे|

1) Single Inheritance

single inheritance को समझना आसान है। जब एक class दूसरे class को extend करती है, तब हम इसे single inheritance कहते हैं। नीचे flow diagram से पता चलता है कि class B केवल एक class को extend कर रही है है जो कि class A है। यहां class A, class B की parent class है ,और class B, class A की child class है|

उदाहरण
Class A
{
public void methodA()
{
System.out.println(“Base class method”);
}
}

Class B extends A
{
public void methodB()
{
System.out.println(“Child class method”);
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}

2) Multiple Inheritance

जब एक class एक से अधिक base class को extend करती है तो इसे Multiple Inheritance कहा जाता है| ” Multiple Inheritance ” के साथ समस्या यह है कि derived class को दो base classes पर निर्भरता का प्रबंधन करना होता है|

नोट 1: सॉफ्टवेयर प्रोजेक्ट्स में Multiple Inheritance का इस्तेमाल बहुत कम किया जाता है। Multiple Inheritance का उपयोग करने से अक्सर hierarchy में समस्याएं होती हैं।

नोट 2: अधिकांश नई OO भाषाएँ जैसे Small Talk, Java, C # Multiple Inheritance का समर्थन नहीं करती हैं। C ++ में Multiple Inheritance का समर्थन किया जाता है।

3) Multilevel Inheritance

Multilevel Inheritance का तात्पर्य ऐसे inheritance से है जहां कोई class, derived class को inherit करती है, जिससे इस derived class को नए class के लिए base class बनाया जा सकता है। जैसा कि आप नीचे flow diagram में देख सकते हैं कि class C, class B का child class है और class B, class A का child class है।

उदाहरण
Class X
{
public void methodX()
{
System.out.println(“Class X method”);
}
}
Class Y extends X
{
public void methodY()
{
System.out.println(“class Y method”);
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println(“class Z method”);
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}

Hierarchical Inheritance

इस तरह की Inheritance में एक base class की कई derived class हो सकती है| नीचे के उदाहरण में कक्षा B, C और D का एक ही base class है। class A, class B,class C & class D का parent class (या base class ) है|

Hybrid Inheritance

सरल शब्दों में आप कह सकते हैं कि Hybrid Inheritance single और multiple inheritance का संयोजन है। एक Hybrid Inheritance जावा में उसी तरह से प्राप्त किया जा सकता है, जैसे मल्टीपल इनहेरिटेंस interface का उपयोग करके प्राप्त किया जा सकता है interface का उपयोग करके आप java में Hybrid Inheritance का उपयोग किया जा सकता है|


error: Content is protected !!