By the end of this, students should be able to:
- Give two reasons why composition and Mixins are desirable.
- Write a class which inherits from another class.
- Describe why composition is preferred to inheritance.
- Fork and clone this repository.
- Install dependencies with
bundle install
.
Classical inheriterance has its limitations and we don't want to limit ourselves, that's where composition comes in. Let's discuss the code below:
class Car
attr_reader :engine
def initialize
@engine = 1200
end
end
class Ford < Car
end
focus = Ford.new
puts focus.engine
Whenever I create a new instance of a Ford
, on initialization an instance of
a Car
is created as well. This instance of car has all of the methods defined
in the Car
class. It inherits all of methods defined in the Car
class.
We want to make chunks of code that are resuable across of multiple classes.
These "chunks" are called modules
. Take a look at the code below:
module Sleeper
def go_to_sleep
# code here
end
end
class Person
include Sleeper
end
class Computer
include Sleeper
end
In the code above we defined a module
called Sleeper. We also define a
Person
class and a Computer
class. By using the keyword include
followed
by the name of the module (in this case Sleeper
) we have access to the methods
we defined in our module. This is great because it allows us to keep our code
D-R-Y, not to mention it allows us to be lazy developers (the good kind of
lazy).
Inheritance creates a subclass - a class that has access to all of the methods of it's parent class. You should use it if your class is a type of it's parent class, like Ford is a type of car. A Ford is a more specialized, less abstract version of the Car class.
Mixins are used when a behavior is shared between various classes. People and computers both share the sleep behavior in the example above. People and computers are very different - it wouldn't make sense for them to inherit from the Sleeper
class, so we use a module to share the behavior.
In a previous
lab,
you were asked to create and use a Shape
class.
A Rectangle
is a Shape
, and a Square
is a Rectangle
.
Create a Rectangle
that inherits from Shape
. You will need to override the
constructor method inside Rectangle
to take two sides of different lengths.
Since all rectangles have four sides, you can set a default value for @sides
inside Rectangle
's constructor.
Requirements for Rectangle
s:
- Rectangles should be instantiated with
Rectangle.new(3, 4)
to create a rectangle with a length of 3 and a width of 4. - Instances of Rectangle should respond to the
#area
method and give the correct result. - Do not override anything that doesn't need to be overriden.
Next, create a Square
class that inherits from Rectangle
.
Requirements for Square
s:
- Squares should be instantiated with
Square.new(4)
to create a square with all sides equal to 4. - Instances of Square should respond to the
#area
method and give the correct result. - Do not override anything that doesn't need to be overriden.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].