Java Inheritance का परिचय (Introduction of Java Inheritance in Hindi)


Java Inheritance का परिचय
(Introduction of Java Inheritance in Hindi)

Inheritance, जावा object oriented programming concept का feature है | जावा में जब एक class के methods को दूसरी class में access किया जाता है तो inheritance कहलाता है |

मान लीजिये आपको प्रोग्राम में एक code को कई जगह इस्तेमाल करना है तो आप कोड को बारबार लिखना पड़ेगा लेकिन जब आप inheritance का प्रयोग करते हैं तो आप एक ही method को एक class से दूसरी class में आसानी से access करा सकते है और इससे आपको कोड बार बार नहीं लिखना पड़ेगा |

Inheritance Reusability को सम्भव बनाता है। इसकी वजह से हम एक ही Class को बार-बार अलग-अलग स्थितियों में उपयोग में ले सकते हैं और हमें बार-बार एक ही प्रकार के Code नहीं लिखने पडते हैं।

inheritance में दो class होती हैं पहली super class और दूसरी sub class |  super class को base class और parent class भी कहा जाता है और sub class को child class और derived class भी कहा जाता है |

जावा  multiple inheritance को सपोर्ट नहीं करता है इसका मतलब ये है कि जावा में parent class के अंदर बहुत सी child classes हो सकती हैं लेकिन child class के कई parent class नहीं हो सकते हैं child class कि सिर्फ एक ही parent class हो सकती है | हाँ इसकी जगह पर आप multi-level inheritance का इस्तेमाल कर सकते हैं |

जावा में inheritance इस्तेमाल करने के लिए extends keyword का इस्तेमाल किया जाता है अगर आप चाहते हैं कि कोई दूसरी class आपकी class को access न करे तो आपको अपनी class के आगे फाइनल keyword लगाना होगा जिन class के आगे फाइनल keyword लगा होता है उन classes को access नहीं किया जा सकता है |

जिस class को inherit किया जाता है वह सुपर class कहलाती है और जो class inherit करती है वह sub class कहलाती है | sub class super class के उन्ही methods को access कर सकती है जो methods या तो public या फिर protected declare किये जाते है जो method private declare किये जाते है उन methods को sub class, super class में से access नहीं कर सकती है |

जावा में inheritance तीन प्रकार की होती है –

  • Single Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance


Single Inheritance
Single Inheritance में एक class किसी दूसरी एक class को ही inherit कर सकती है |

Example of Single Inheritance

class A{
void disp(){
                 System.out.println("Parent Class");
}
}
class B extends A{
void show(){
                 System.out.println("Child Class");
}
public static void main(String args[]){
B obj = new B();
obj.disp();
obj.show();
}
}


Multilevel Inheritance
Multilevel Inheritance में एक class दूसरी class को inherit करती है और दूसरी class तीसरी class को inherit करती है|

Example of Multi-level Inheritance

class A{
               
                void disp(){
                                System.out.println("Class A");
                }
}
class B extends A{
                void show(){
                                System.out.println("Class B");
                }
}
class C extends B{
                void getdata(){
                                System.out.println("Class C");
                }
                public static void main(String args[]){
                C obj = new C();
                obj.disp();
                obj.show();
                obj.getdata();
                }
}


Hierarchical Inheritance
Hierarchical Inheritance में एक class को बहुत सी classes inherit करती है जैसे नीचे दिए गए चित्र में |




 Example of Hierarchical Inheritance
class A{
                void disp(){
                                System.out.println("Class A");
                }
}
class B extends A{
                void show(){
                                System.out.println("Class B");
                }
}
class C extends A{
                void getdata(){
                                System.out.println("Class C");
                }
                public static void main(String args[]){
                C obj = new C();
                obj.disp();
                obj.getdata();
                B b = new B();
                b.show();
                }
}


Java Inheritance का परिचय (Introduction of Java Inheritance in Hindi) Java Inheritance का परिचय  (Introduction of Java Inheritance in Hindi) Reviewed by Unknown on October 12, 2018 Rating: 5

No comments:

Theme images by fpm. Powered by Blogger.