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

Recent Posts

Build Simple Water System Calculator in Java Using Swing

If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…

2 months ago

GitHub Copilot vs Microsoft Copilot Best AI Tool to Use in 2025

GitHub is a powerful tool used by teams and developers around the globe. This guide is…

2 months ago

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

2 months ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

2 months ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

2 months ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

2 months ago