From 1512265be70e96b77407d723cccb958ac61cf6f5 Mon Sep 17 00:00:00 2001 From: Matt Follett Date: Sun, 29 Aug 2010 23:58:49 -0500 Subject: [PATCH] Added a section on single inheritance --- src/classes-and-objects.pod | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/classes-and-objects.pod b/src/classes-and-objects.pod index 96fba8a..ff07688 100644 --- a/src/classes-and-objects.pod +++ b/src/classes-and-objects.pod @@ -348,6 +348,41 @@ the various other dependencies in order, giving the output: making dinner eating dinner. NOM! +=head1 Inheriting the Farm + +Object Oriented Programming provides the concept of inheritance as one of the +mechanisms to allow for code reuse. Perl 6 supports the ability for one class +to inherit from multiple classes. When a class inherits from another class it +acquires all of the attributes and methods of the base class. + +=begin programlisting + +class Farmer +{ + has $.farm is ro; + + method plant_crops( Str $crop ) + { + "Planting some $crop at the farm" + } +} + +class FarmerKid is Farmer +{ +} + +=end programlisting + +Now all the objects of type FarmerKid will inherit the base class' farm +attribute and plant_crops method as though they were declared on FarmerKid. + +=begin programListing + +my $fk = new FarmerKid( farm => 'across from the dairy'); + +$fk.plant_crops('wheat'); + + =head1 Exercises B<1.> The method C in C permits the creation of I