Categories: Uncategorized

Mastering JavaScript OOP Inheritance

Inheritance in JavaScript using classes. You have a Person class as the superclass, an Employee class  that extends Person, and two subclasses (Designer and Developer)

Person Class:

  • it has a constructor that takes a name parameter and initializes the name property.
  • Defines a play method.

Employee Class:

  • Extends the Person class.
  • it has a constructor that takes a name and an id parameter, and initializes the id property.

Designer Class:

  • Extends the Employee class.
  • it has a constructor that takes a name and an id parameter.
  • Defines a design method.

Developer Class:

  • Extends the Employee class.
  • it has a constructor that takes a name and an id parameter.
  • Defines a coding method.

Creating instances and invoking methods:

  • Creates instances of Developer and Designer.
  • Calls the play, coding, and design methods.
class Person     //super class 
{
    constructor(name)
    {
        this.name=name
    

    this.play=()=>{
        console.log("playing");
    }
}
}

//sub class
class Employee extends Person{
    constructor(name,id)
    {
        super(name)
        this.id=id
     }
}

class Designer extends Employee{
    constructor(name,id)
    {
        super(name,id)
        this.design=()=>{
            console.log("Designing.......")
        }
        
     }
}

class Developer extends Employee{
    constructor(name,id)
    {
        super(name,id)
        this.coding=()=>{
            console.log("Coding.......")
        }
        
     }
}

const developer = new Developer("java codinggg",1)
const designer = new Designer("UX Design",1)

developer.play()
developer.coding()
designer.design()

 

admin

Share
Published by
admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

6 days ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

4 weeks ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

1 month ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

2 months ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago