September 2017
Beginner
402 pages
9h 52m
English
In many cases, the methods of child classes should react differently from the methods in parent classes. Here we come to an important concept of redefining or overriding methods.
Let us continue an example with House and ModernHouse and implement the method to calculate the cost of electricity needed to warm the house. To focus on the main topic, we simplify the classes and the method of calculating the cost, assuming that it is proportional to the area of the house.
In both classes, we define the method named energy_cost, which does different calculations for each class:
class House { has $.area; method energy_cost() { return 0.8 * $!area; }}class ModernHouse is House { has $.power; method energy_cost() { return 0.3 * ...
Read now
Unlock full access