Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Class Inheritance

JavaScript OOP

Telerik Software Academy

http://academy.telerik.com

Table of Contents

Inheritance

  • Inheritance allows child classes to inherit the characteristics of an existing parent (base) class
    • Attributes (fields and properties)
    • Operations (methods)
  • Child class can extend the parent class
    • Add new fields and methods
    • Redefine methods (modify existing behavior)
  • Inheritance has a lot of benefits
    • Extensibility
    • Reusability (code reuse)
    • Provides abstraction
    • Eliminates redundant code
  • Use inheritance for building is-a relationships
    • E.g. dog is-a animal (dogs are kind of animals)
  • Don't use it to build has-a relationship
    • E.g. dog has-a name (dog is not kind of name)
  • Inheritance implicitly gains all members from another class
    • All fields, methods, properties, events, …
    • Some members could be inaccessible (hidden)
  • The class whose methods are inherited is called base (parent) class
  • The class that gains new functionality is called derived (child) class

Classes and Inheritance in ES6

  • ES6 introduces classes and a way to create classical OOP
    • Using the class keyword
class Mammal {
	constructor(age) {
		this._age = age;
	}
}
  • Sub classing is done using the extend keyword
class Mammal {
	speak(str) {
		// mammals usually don't speak
	}
}

class Person extends Mammal {
	speak(str) {
		console.log(str);
	}
}
  • super is used to refer to the parent class
    • super() calls the parent constructor
      • needed in order for this to refer to the correct object
    • super.method() calls .method() from the parent class
class Person extends Mammal {
  constructor(fname, lname, age) {
    super(age);
    this._fname = fname;
    this._lname = lname;
  }
  get fullname() {
    // getter property of fullname
  }
  set fullname(newfullname) {
    // setter property of fullname
  }
  // more class members…
}
Constructor of the class
Getters and setters

Free Trainings
@ Telerik Academy