23: Inheritance

In the previous exercise (and video) you learned about the concept of a class. A class allows you to encapsulate an interface in a single type so that other code expecting that type can work correctly without having to know about your implementation. In theory you should be able to take any objects of the same class and use them in any code expecting objects of that class.

Inheritance takes the concept of a class and allows you to make other "similar" classes by way of extending them in a parent and child relationship. You take a class you want to change, extend it with your new functionality, and you can use it anywhere the original can be used. This has advantages in saving you from having to implement code that someone else already wrote, allows you to work with existing code, and gives you some assurances that it will keep working.

Code Reuse

The first advantage of inheritance is to allow you to base new classes on old classes by only writing code to describe what's new. You can extend a Guitar class with a new Telecaster class rather than write a whole new Guitar class just so you can get a Telecaster class:

Learn JavaScript Today

Register today for the Beginner Basics course and get the new releases by just paying the difference. There's also special discounts for existing customers when new courses are released.

Still Not Sure? Check out more curriculum!