skip to Main Content
class Person{
  constructor(name){
    this.name = name;
  }
}

class Financial extends Person{
  constructor(salary){
    this.salary = salary;
  }
}

// demand and debt class
class DD extends Financial{
  constructor(amount){
    this.amount = amount;
  }
}
let jack = new Person('jack');

I have 3 classes which inherent each other.
I made an instance of first class(person),
now how can I make a new instance of DD class for jack?

I’m making a simple Accounting webApp and this is my problem.

2

Answers


  1. Chosen as BEST ANSWER

    class Person {
      constructor(name) {
        this.name = name;
      }
    }
    
    class Financial extends Person {
      constructor(name, salary) {
        super(name); // Call the constructor of the parent class
        this.salary = salary;
      }
    }
    
    class DD extends Financial {
      constructor(name, salary, amount) {
        super(name, salary); // Call the constructor of the parent class
        this.amount = amount;
      }
    }
    
    let jack = new Person('Jack');
    
    //My new code
    jack = new DD(jack.name,200,1350);
    
    console.log(jack.amount);

    I found the solution.


  2. To create a new instance of the DD class for the existing instance jack, you need to follow the inheritance chain and make sure that the constructors of each class are called properly. In your current implementation, the constructors of the child classes (Financial and DD) are not calling the constructors of their parent classes (Person and Financial). To achieve this, you can use the super keyword inside the child class constructors.

    Here’s the updated implementation of your classes:

    class Person {
      constructor(name) {
        this.name = name;
      }
    }
    
    class Financial extends Person {
      constructor(name, salary) {
        super(name); // Call the constructor of the parent class
        this.salary = salary;
      }
    }
    
    class DD extends Financial {
      constructor(name, salary, amount) {
        super(name, salary); // Call the constructor of the parent class
        this.amount = amount;
      }
    }
    
    let jack = new Person('Jack');
    let jackAccount = new DD('Jack', 5000, 1000);
    
    console.log(jackAccount);
    

    In this example, we added super(name, salary) in the constructor of Financial to call the constructor of Person, passing the name argument to it. Similarly, in the constructor of DD, we used super(name, salary) to call the constructor of Financial, passing both name and salary arguments to it.

    Now, you can create a new instance of DD for the existing instance jack by passing the required arguments (name, salary, and amount) to the DD constructor as shown in the jackAccount instantiation above. This way, you can maintain the inheritance chain and create instances for your classes accordingly in your Accounting webApp.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search