Inheritance
The next feature of object-oriented programming is inheritance. In this section, we talk about inheritance and related topics in Perl 6.
Inheriting from a class
Inheriting in OOP means creating a new class, which extends another already existing class. The simplest form of inheritance is a child-parent pair of two classes.
In the previous sections, we created the House
class. Let us use it as the parent class for another concept. We will create a ModernHouse
class, which is a House
with a solar roof panel. A bare House
, which we created earlier in this chapter, contains four attributes—number of rooms, area, height, and address. The address attribute was an Address
object in our previous examples but, in this section, we will keep it simple and assume that the address is a string:
class House { has $.rooms; has $.area; has $.height; has $.address; }
For a ModernHouse
, another attribute, the power that the solar panel generates, is added:
class ModernHouse is House ...