Working with JavaScript Class Inheritance:-
Class inheritance is a fundamental concept in object-oriented programming languages such as JavaScript. It allows one class to inherit the properties and methods of another class. This type of inheritance is known as prototypal inheritance, which allows objects to share behavior and properties with other objects.
In JavaScript, class inheritance works by creating a new object based on an existing object. This new object has access to all of the methods and properties of the original object, as well as any additional methods and properties that are defined for it.
Class inheritance is an important concept for understanding how objects work in JavaScript, and it is essential for creating complex, reusable code. In order to use class inheritance effectively, developers need to understand how objects, properties, and methods work in JavaScript. They also need to understand how to use the prototype chain to access inherited methods and properties.
When creating a class, developers must define both the class itself and any methods or properties that are associated with it. Once the class is defined, developers can create a new object based on the class. This object can then be used to access the class’s properties and methods.
Class inheritance can also be used to create objects that are based on existing objects. For example, if a developer wants to create a custom object based on an existing object, they can use class inheritance to do so. This allows the developer to reuse code and make the custom object more efficient.
In addition to creating custom objects, class inheritance can also be used to extend existing objects by adding additional methods and properties. This can be done by creating a new object based on the existing object and then adding the new methods and properties to it.
Overall, class inheritance is an important concept in object-oriented programming languages such as JavaScript. It allows developers to create complex, reusable code by allowing objects to share behavior and properties with other objects. Developers should understand how objects, properties, and methods work in JavaScript, as well as how to use the prototype chain to access inherited methods and properties in order to use class inheritance effectively.
Steps To Do ClassInheritance:-
1. Create a superclass for the class you want to inherit from. This class should contain all the methods and attributes that you would like to be inherited by the subclass.
2. Create a subclass that extends the superclass.
3. Override any methods you would like to customize in the subclass.
4. Use the subclass in your program wherever you would like to use the inherited methods and attributes.
5. Make sure that the subclass has a constructor that calls the superclass constructor. This is necessary in order to properly inherit attributes from the superclass.
Example:-1
class ParentClass {
constructor() {
this.name = "Parent Class";
}
parentMethod() {
console.log("I am the Parent Class");
}
}
class ChildClass extends ParentClass {
constructor() {
super(); // Calls parent constructor
this.name = "Child Class";
}
childMethod() {
console.log("I am the Child Class");
}
}
const child = new ChildClass();
child.parentMethod(); //Parent Class
child.childMethod(); //Child Class
Example:-2
class employee{
constructor(name,age,salary){
this.name=name;
this.age=age;
this.salary=salary;
}
info(){
document.write(
`<h3>Employee details</h3>Name:-
${this.name} <br>
Age:-${this.age}
<br>Salary:-
${this.salary}`);
}
}
class manager extends employee{
info(){
let ta=1000;
let pa=1000;
let totalsal=this.salary+ta+pa;
document.write(
`<h3>Manager details:</h3>Name:-
${this.name} <br>
Age:-${this.age}
<br>Salary:-${totalsal}`);
}
}
let obj1=new manager("Shivam",30,20000);
let obj2=new employee("Seetu",30,20000);
obj1.info();
obj2.info();
**********************************
Happy to see you hereππ.
**********************************
visit www.javaoneworld.com for more posts.
*********************
No comments:
Post a Comment