October 2019
Intermediate to advanced
434 pages
11h 54m
English
Inheritance is the notion that one class can inherit data and methods from another class. This can allow groups of related classes to avoid duplication of critical code or data, and can enforce strict relationships between related things.
Building on the previous example, we see in the following that we can define a Programmer class that inherits from Person:
class Programmer(firstName: String, lastName: String, val favoriteLanguage: String) : Person(firstName, lastName)
This makes logical sense as a programmer is a person. In this case, Programmer adds a favoriteLanguage property while inheriting the firstName and lastName properties from its base class, Person.
Read now
Unlock full access