Inheritance
It is used to indicate that one class will get most or all of its features from a parent class. This happens implicitly when you write class Baby < Mama which says that a class Baby inherits from Mama. This is great to inherit the behaviors to Baby from Mama, with the ability to override or add behaviors.
Example of inheritance:
Composition
There is an alternative way of doing a very similar thing. In the example below, instead of using class Mama, I used class Family and brought in from of the functionality of Family by initializing a variable in the class Baby. this would be useful if we had multiple family members with with some similarities with which we would not have to repeat the same code, and extend this similarities to many different classes if need be.
Example of Composition:
So, when do we use inheritance or composition?
Since we want the code to be DRY, you Don't want to Repeat Yourself, so identify what code is repeated or duplicated and think about how the code code be manipulated in the future and why/how the code is going to be accessed.
July 16, 2014