JavaScript Inheritance In Hindi | JS Inheritance Explained In Hindi

📔 : Java Script 🔗

किसी भी OOP programming में Inheritance बहुत ही important concept है।

किसी class को या class की properties / behavior को extend करना Inheritance कहलाता हैं। जिस class को extend किया गया है उसे Parent / Base Class , जिस class के द्वारा extend किया गया है उसे Child / Derived Class कहते हैं।

Inherit करने के लिए extend keyword का use किया जाता है और Extend करने पर Child Class में लगभग बो सभी properties / methods होंगे जो Parent Class में हैं ।

JS Inheritance Example

// Base/Parent class. class A { prop_name = "property of class A"; methodA() { return "method of class A"; } } // empty Child/Derived class inheriting parent class A. class B extends A { } // now access parent class's data using child class. const inst = new B(); console.log(inst.prop_name); console.log(inst.methodA());

Output :

property of class A
method of class A

example में आप देख सकते हैं , कि कैसे class A द्वारा B class के method और property को access किया।

इसी तरह से जो भी properties / methods parent class में मौजूद होगीं उन सभी को class के अंदर किसी method में या बाहर child class द्वारा access किया जा सकता है।

JS access parent class's member in child class's method

अब हालाँकि Extend करने पर Child Class में लगभग बो सभी properties / methods होंगे जो Parent Class में हैं , जिसका मतलब है आप उन members को this keyword का use करके भी access कर सकते हैं।

class A { myproperty = "property of class A"; method_a() { return "method of class A" } } class B extends A { show() { // access parent class's members using `this`. console.log(this.myproperty); console.log(this.method_a()); } }

Output :

property of class A
method of class A

JS super keyword

अब जिस तरह से this का use करके current class की सभी actual या extended properties / method को access कर सकते हैं , ठीक उसी तरह से super keyword का use करके आप parent class के methods को access कर सकते हैं।

और super() method का use करके parent class के constructor को call कर सकते हैं।

class A { constructor() { console.log("Parent class constructor."); } method_a() { return "method of class A" } } class B extends A { constructor() { // call parent class's contructor. super(); console.log("Child class constructor."); } show() { // access parent class's method using `super`. console.log(super.method_a()); } } const inst = new B(); inst.show();

Output :

Parent class constructor.
Child class constructor.
method of class A

JS access parent class's static member in child class's method

जिस तरह से class name का use करके current class की सभी actual या extended static properties / method को access कर सकते हैं , ठीक उसी तरह से parent class name का use करके आप parent class के सभी static properties / method को child class में access कर सकते हैं।

For example :

class A { // define static propert & method. static myproperty = "static property of class A"; static method_a() { return "static method of class A" } } class B extends A { // access parent class's static members using class name `A`. show_A() { console.log(B.myproperty); console.log(B.method_a()); } show_B() { // access parent class's members using class name `A`. console.log(A.myproperty); console.log(A.method_a()); } } const inst = new B(); inst.show_A(); inst.show_B();

जैसे कि ऊपर दिए गए example में आप देख सकते हैं , कि non-static method के अंदर child & parent class name का use करके static member को access कर सकते हैं।

Remember : आप non-static method के अंदर तो this , super या class name का use करके किसी भी तरह की actual या extended properties / method को access कर सकते हैं , लेकिन static method के अंदर आप this & super का use नहीं कर सकते हैं।

I Hope guys , आपको JavaScript में Inheritance का concept अच्छे से समझ आया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers