Imagine our doggy daycare is so successful that we decide to expand the business and open a kitty daycare. Before the daycare opens, we need to create a Cat
class so we can quickly generate Cat
instances. We know that the properties in our Cat
class (name
, behavior
) are similar to the properties in our Dog
class, though, there will be some differences, because of course, cats are not dogs.
Let’s say that our Cat
class looks like this:
class Cat { constructor(name, usesLitter) { this._name = name; this._usesLitter = usesLitter; this._behavior = 0; } get name() { return this._name; } get usesLitter() { return this._usesLitter; } get behavior() { return this._behavior; } incrementBehavior() { this._behavior++; } }
In the example above, we create a Cat
class. It shares a couple of properties (_name
and _behavior
) and a method (.incrementBehavior()
) with the Dog
class from earlier exercises. The Cat
class also contains one additional property (_usesLitter
), that holds a boolean value to indicate whether a cat can use their litter box.
When multiple classes share properties or methods, they become candidates for inheritance — a tool developers use to decrease the amount of code they need to write.
With inheritance, you can create a parent class (also known as a superclass) with properties and methods that multiple child classes (also known as subclasses) share. The child classes inherit the properties and methods from their parent class.
Let’s abstract the shared properties and methods from our Cat
and Dog
classes into a parent class called Animal
.
class Animal { constructor(name) { this._name = name; this._behavior = 0; } get name() { return this._name; } get behavior() { return this._behavior; } incrementBehavior() { this._behavior++; } }
In the example above, the Animal
class contains the properties and methods that the Cat
and Dog
classes share (name
, behavior
, .incrementBehavior()
).
The diagram to the right shows the relationships we want to create between the Animal, Cat, and Dog classes.