Inheritance in Javascript in Hindi
एक अच्छे program का rule है Do not repeat yourself(जितनी जरुरत है उतना ही code लिखें) | इस concept को पाने के लिए OOP में inheritance एक मुख्य भूमिका निभाता है |
अगर एक class में properties, functionalities define है और उसी functionalities को दुसरे class में जरुरत है तो उसे inherit कर सकतें हैं |
Inheritance का अर्थ है एक object अपनी feature को extend करता है और दुसरे object की functions को access कर सकता है | javascript के ES6 version में inheritance, class के जरिए की जाती है |
class inheritance के लिए extends keyword का उपयोग होता है |
उदाहरण
class Department{ constructor(departmentName, name){ this.departmentName = departmentName; this.name = name; } greet(){ console.log(`Hello '${this.name}' your Department name is '${this.departmentName}'`) } } class Employee extends Department{ } var obj = new Employee("IT", "Roshan"); obj.greet();
output
super keyword
parent class के constructor को child class में call करने के लिए super keyword का उपयोग किया जाता है | super keyword के जरिए parent class की properties और methods को access की जा सकती है, उसमें values भेजी जा सकती है |
उदाहरण
class Department{ constructor(departmentName){ this.departmentName = departmentName; } } class Employee extends Department{ constructor(name, departmentName, post){ super(departmentName); this.name = name; this.post = post; } dispayDetails(){ console.log("Employee Name: "+ this.name); console.log("Department Name: "+ this.departmentName); console.log("Employee Designation: "+ this.post); } } var obj = new Employee("Roshan", "IT", "Software Engineer"); obj.dispayDetails();
उदाहरण में देखिए Department parent class है और इसमें departmentName की property को value इसकी Employee नाम की child class में दी गयी है | इसके लिए super keyword का उपयोग किया गया है |
output
Types of Inheritance
Single Level Inheritance
जब एक child class सिर्फ एक ही parent class से inherit होता है, तब उसे single level inheritance कहतें हैं |
उदाहरण
class Department{ constructor(){ this.department = "IT"; } getDeptName(){ console.log("Department Name is:" + this.department); } } class Employee extends Department{ constructor(){ super(); this.name = "Kiran"; } getName(){ console.log("Employee Name is: " + this.name); } getDeptName(){ console.log("Department Name is: " + this.department) } }
output
Multi Level Inheritance
जब एक child class दुसरे child class से derived होता है और वही derived class, दुसरे parent class से derive हुआ होता है | ऐसे inheritance को multi level inheritance कहतें हैं |
इस inheritance में एक class से दुसरे classes से parent, grant-parent relationship बनाता है |
उदाहरण
class Department{ constructor(){ this.department = "IT"; } getDeptName(){ console.log("Department Name is:" + this.department); } } class Employee extends Department{ constructor(){ super() this.name = "Kiran"; } getName(){ console.log("Employee Name is: " + this.name); } } class Occupation extends Employee{ constructor(){ super(); this.job = "Software Engineer"; } getOccupation(){ console.log("Your Ocuupation is: " + this.job); } } var obj1=new Occupation(); obj1.getDeptName(); obj1.getName(); obj1.getOccupation();
output
Multiple Inheritance
जब एक child class एक से अधिक parent classes से derived होता है, ऐसे inheritance को multiple inheritance कहतें हैं |
नॉटपॉइंट: पर javascript multiple inheritance को support नहीं करता है |
static object inheritance
javascript में एक static object दुसरे static object की properties और methods को inherit कर सकता है |
जब एक object बनता है, तब उसमें __proto property by-default बन जाता है | __proto property के जरिए एक object दुसरे object को inherit कर सकता है |
Syntax: childObject.__proto__ = parentObject
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Static object Inheritance Example</title> </head> <body> <script> var class1 = { classes:"IV", section:"A" } var student ={ name:"Rani", age:13 } student.__proto__= class1; console.log("Student Class is " + student.classes); console.log("Student Section is " + student.section); console.log("Student Name is " + student.name); </script> </body> </html>
उदाहरण में देखिए student object __proto__ के जरिए class1 object की property को inherit कर रहा है |
output