
Inheritance in JavaScript
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. In JavaScript, inheritance enables code reusability and promotes a hierarchical structure among classes. Let's delve into the concept of inheritance in a way that's easy for a fifth-grade student to understand.
Inheritance in JavaScript refers to the concept where a new class or object can acquire the properties and methods of an existing class or object. This allows for code reuse and the creation of hierarchical relationships between objects.
First, define the parent class with its properties and methods. This class will serve as the template for the child classes to inherit from.
Next, create child classes that extend the parent class using the 'extends' keyword. These child classes will inherit properties and methods from the parent class and can also have their own unique properties and methods.
Instantiate objects of the child classes and use their inherited methods as well as their own methods.
In this example, 'Dog' and 'Cat' are child classes that inherit from the 'Animal' parent class. They inherit the 'name' property and 'makeSound()'' method from the 'Animal' class, and they also have their own unique methods ('bark()'' for 'Dog' and 'meow()'' for 'Cat'). When you create instances of 'Dog' and 'Cat', you can use both inherited methods and the methods specific to each subclass.
Class name: employee Data members: String name, float basic; Member functions: void getdata(): to accept the name and basic pay Sub Class salary inherit Class employee . Data members : float da, hra, gross, net; Member Functions: void calculate(): to find the followings: da=50% of basic , hra= 10% of basic , gross= basic + da + hra, pf=8.33% of (basic+da) net = gross – pf. void display() : to display the playslip
Output :
NAME=Rokeiya Sultana
BASIC SALARY OF EMPLOYEE=15000
DA=7500
HRA=1500
GROSS=24000
PF=8745
NET SALARY=15255
Inheritance is a fundamental concept in JavaScript that enables objects to acquire properties and behaviors from other objects.