Introduction
Class inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class.
The class whose members are inherited is called the parent class (or base class), and the class that inherits them is called the child class (or derived class).
Inheritance promotes code reusability, reduces duplication, and makes applications easier to maintain. Instead of rewriting the same code in multiple classes, you can define common functionality in a parent class and reuse it in child classes.
In Node.js, inheritance is implemented using the extends keyword.
For automation engineers, inheritance is widely used in automation frameworks such as Playwright, Selenium, and Cypress. Common functionalities like browser setup, page navigation, logging, and utility methods are often placed in a base class and inherited by page classes.
In this tutorial, you’ll learn the concept of class inheritance and how it works in Node.js.
What is Class Inheritance?
Class inheritance is the process by which one class acquires the properties and methods of another class.
It allows developers to create new classes based on existing ones.
Example:
Parent Class: Employee
Child Class: Manager
The Manager class automatically inherits the features of the Employee class.
Why Use Class Inheritance?
Class inheritance helps developers:
Reuse existing code.
Reduce code duplication.
Improve maintainability.
Create hierarchical relationships.
Extend existing functionality.
Organize code efficiently.
Build scalable applications.
Syntax
class ParentClass {
}
class ChildClass extends ParentClass {
}
The child class inherits all accessible properties and methods from the parent class.
Example 1: Basic Class Inheritance
class Animal {
eat() {
console.log(
"Animal is eating."
);
}
}
class Dog extends Animal {
}
const dog =
new Dog();
dog.eat();
Sample Output
Animal is eating.
The Dog class inherits the eat() method from the Animal class.
Example 2: Parent and Child Methods
class Employee {
work() {
console.log(
"Employee is working."
);
}
}
class Manager extends Employee {
manage() {
console.log(
"Manager is managing."
);
}
}
const manager =
new Manager();
manager.work();
manager.manage();
Sample Output
Employee is working.
Manager is managing.
Example 3: Multiple Child Objects
class Vehicle {
start() {
console.log(
"Vehicle started."
);
}
}
class Car extends Vehicle {
}
const car1 =
new Car();
const car2 =
new Car();
car1.start();
car2.start();
Sample Output
Vehicle started.
Vehicle started.
Example 4: Child Class Adds New Features
class Person {
walk() {
console.log(
"Walking..."
);
}
}
class Student extends Person {
study() {
console.log(
"Studying..."
);
}
}
const student =
new Student();
student.walk();
student.study();
Sample Output
Walking...
Studying...
Example 5: Inheriting Properties
class User {
constructor() {
this.role = "User";
}
}
class Admin extends User {
}
const admin =
new Admin();
console.log(admin.role);
Sample Output
User
Example 6: Real-World Example
Represent employees in an organization.
class Employee {
login() {
console.log(
"Employee logged in."
);
}
}
class Developer extends Employee {
writeCode() {
console.log(
"Writing code..."
);
}
}
const developer =
new Developer();
developer.login();
developer.writeCode();
Sample Output
Employee logged in.
Writing code...
Automation Testing Example
Inheritance is extensively used in automation frameworks.
Playwright Example
Create a reusable base page.
class BasePage {
openApplication() {
console.log(
"Application opened."
);
}
}
class LoginPage extends BasePage {
}
const loginPage =
new LoginPage();
loginPage.openApplication();
Selenium Example
Reuse browser setup.
class BaseTest {
launchBrowser() {
console.log(
"Browser launched."
);
}
}
class LoginTest extends BaseTest {
}
const test =
new LoginTest();
test.launchBrowser();
Cypress Example
Share common methods.
class BaseCommands {
visitHomePage() {
console.log(
"Homepage opened."
);
}
}
class DashboardPage extends BaseCommands {
}
const dashboard =
new DashboardPage();
dashboard.visitHomePage();
API Testing Example
Reuse API utilities.
class BaseApi {
sendRequest() {
console.log(
"Request sent."
);
}
}
class UserApi extends BaseApi {
}
const api =
new UserApi();
api.sendRequest();
Data-Driven Testing Example
Reuse employee information.
class Employee {
showDepartment() {
console.log(
"IT Department"
);
}
}
class Tester extends Employee {
}
const tester =
new Tester();
tester.showDepartment();
Common Mistakes
Forgetting the extends Keyword
Incorrect:
class Dog Animal {
}
Correct:
class Dog extends Animal {
}
Expecting Access Without Inheritance
class Animal {
eat() {
console.log("Eating");
}
}
class Dog {
}
const dog =
new Dog();
dog.eat();
This results in an error because Dog does not inherit from Animal.
Duplicating Common Code
Instead of writing the same methods in multiple classes, place common functionality in a parent class and inherit it.
Best Practices
Place common functionality in parent classes.
Keep parent classes generic and reusable.
Extend classes only when an “is-a” relationship exists.
Avoid unnecessary inheritance.
Use inheritance to reduce code duplication.
Keep child classes focused on specialized behavior.
Design inheritance hierarchies carefully.
Conclusion
Class inheritance is one of the most powerful features of Object-Oriented Programming in Node.js. It enables code reuse by allowing child classes to inherit properties and methods from parent classes.
For automation engineers, inheritance is essential when implementing reusable page objects, browser utilities, API clients, and common test functionality in automation frameworks.
Understanding class inheritance provides a strong foundation for learning advanced concepts such as the extends keyword, the super keyword, method overriding, and polymorphism.
Frequently Asked Questions (FAQs)
What is class inheritance?
Class inheritance is the process where one class inherits the properties and methods of another class.
Which keyword is used for inheritance in JavaScript?
The extends keyword.
What is a parent class?
A parent class is the class whose properties and methods are inherited by another class.
What is a child class?
A child class is a class that inherits from a parent class.
Why is inheritance important in automation testing?
Inheritance helps create reusable base classes for browser setup, page objects, API utilities, logging, and common test methods, reducing code duplication and improving maintainability.
Key Takeaways
Class inheritance allows one class to inherit another class’s properties and methods.
Use the
extendskeyword to implement inheritance.Parent classes contain common functionality.
Child classes inherit and extend parent class features.
Inheritance reduces code duplication.
It improves code organization and maintainability.
Inheritance is widely used in Playwright, Selenium, Cypress, and API testing frameworks.
Use inheritance only when an appropriate parent-child relationship exists.
Keep parent classes reusable and child classes specialized.
Understanding inheritance is essential before learning
super, method overriding, and polymorphism.
