![]() |
Sub-class: A subclass is a class that is derived from the properties and methods of some other class known as the Parent class for that subclass. A subclass allows us to change or update the properties of the parent class without disturbing it. A subclass can contain properties of the parent class as well as we can define the new properties inside it. Here, In the above picture, the GeeksforGeeks class will act as the parent class for both Officials and Geeks class. The Officials and Geeks class will be subclasses and they inherit some properties and methods from the parent class GeeksforGeeks. To provide properties of the parent class to a subclass we use inheritance: Inheritance is the way of extending a class by providing it some new properties and values using another class without even disturbing it. Inheritance can be done in two ways:
Prototypal inheritance: The prototypal inheritance is done using the prototype keyword. The prototypal inheritance is the es5 syntax of inheritance. Syntax: function func_name(){ // Content of func_name() } func_name.prototype.func_name2(){ // Content of func_name2() } Example: The example below illustrates the inheritance using the prototype keyword. Javascript
Output: Inheritance using extends keyword: es6 or ECMAScript-2015 introduces the concept of inheriting the properties of the parent class using the extends keyword. We will use the super() method inside the subclass to invoke the constructor function of the parent class. Syntax: // Code for the parent class class parent_class{ constructor(){ // Body of constructor } } // Code for the child or sub class class sub_class extends parent_class{ constructor(){ super() // Body of constructor } } Example: Javascript
Output: |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |