Inheritance


ES6 allows you to override, extend and manipulate your classes When extending classes you have access to the methods within the super class Extends allows us to use all the features and methods that the parent class creates Example : Extending the Person class

//es6 Classes and Inheritence


class Person {
  constructor(name) {
    this.name = name
  }
  
  greet(){
    console.log('Booshys baby sister is ' + this.name);
  }
}

//Adding, extending and overriding
class Violet extends Person {
  
}


let baby = new Violet('Violet');

//Because the class Violet extends the person class you still have access to the greet method
baby.greet();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Example : Adding to the Babies Class

  • Note If you want to use a constructor in a class you must also call the constructor in the parent class

//This example will throw an error because we havent called the parent class constructor


class Person {
  constructor(name) {
    this.name = name
  }
  
  greet(){
    console.log('Booshys baby sister is ' + this.name + ' and I am ' + this.age);
  }
}

//Adding, extending and overriding
class Violet extends Person {
  constructor(age) {
    this.age = age;
  }
  
}


let baby = new Violet(27);

//Because the class Violet extends the person class you still have access to the greet method
baby.greet();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Example : Using the super keyword to call the parent class constructor

//es6 Classes and Inheritence


class Person {
  constructor(name) {
    this.name = name
  }
  
  greet(){
    console.log('Booshys baby sister is ' + this.name + ' and I am ' + this.age);
  }
}

//Adding, extending and overriding
class Violet extends Person {
  constructor(age) {
    super('Violet');//Calling the parent constructor
    this.age = age;
  }
  
}


let baby = new Violet(27);

//Because the class Babies extends the person class you still have access to the greet method
baby.greet();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Example : Extending classes and using methods of the parent class

//es6 Classes and Inheritence


class Person {
  constructor(name) {
    this.name = name
  }
  
  greet(){
    console.log('Booshys baby sister is ' + this.name + ' and I am ' + this.age);
  }
}

//Adding, extending and overriding
class Violet extends Person {
  constructor(age) {
    super('Violet');//Calling the parent constructor
    this.age = age;
  }
  
  //Adding Additional Methods
  greetTwice() {
    this.greet();
    this.greet();
    
  }
  
  //Alternatively you can use the super keyword
  //greetTwice() {
  //  super.greet();
  //  super.greet();
    
  //}
  
}


let baby = new Violet(27);

//Because the class Babies extends the person class you still have access to the greet method
baby.greetTwice();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Last Updated: 8/13/2019, 6:55:26 PM