Composition vs. Inheritance
somebody was once making some objects and noticed they all had something in common so they made some kind of parent object that these objects could “inherit” from — they are really vague definitions, in Vulcan you can see a lot of classes inherit from a base class


Example of Inheritance
Notice all the child classes get all the "shapes" of its parent class -- in programming that would be similar to reusing properties and methods of the base class
Two types of inheritance
Classical Inheritance
Prototypal Inheritance
- Inheriting from a parent class
- Inheritance is made at compile time
example: inheritance in java, C#
- Inheriting from an instance of the class: an object
- Inheritance is made on the fly/ dynamically meaning that you are really going back to the super class for those properties, saves on memory
example: Javascript
Example of Inheritance: Animal Kingdom
Animal
Fish
Birds
Mammals
Dogs
People
Example of Inheritance: Animal Kingdom
Animal
Fish
Birds
Mammals
Dogs
People
Platypus
??
Example of Inheritance: Animal Kingdom
Fish
Birds
Mammals
Dogs
People
Platypus
??
with inheritance this can be a problem because if some subclass doesn't fit entirely into the superclasses: what can you do ?
What can you do?
Two options:
1. redefine the class above it, mammals that lay eggs and mammals that give live birth
2. override the method of gives_birth() within that platypus subclass only

recommendations when using inheritance:
- make sure it passes the IS-A vs. HAS-A test
- keep the inheritance hierarchy shallow
- use POCOs — plain old class objects, without methods on them, just properties (eg: using properties that will always be relevant like first name and last name for a class Person()
- now we are able to pass this Animal() to a controller who acts on the Animal() and saves the changes — example of using composition with Inheritance
Using Composition
- this is another way to get reusability out of other classes
- not a hierarchy structure
- each composition class can be very specific

Managing the Complexity — best practice is to use a combination of the two
A TacoTruck() with multiple roles/responsibilities:
- have a TacoTruck class with a property on it called roles: [list]
- have a Role class with a shallow inheritance structure for when another class has more than one role

- have a property on TacoTruck class called roles
- have a Role class with a shallow inheritance that can be combined into a composiition structure when a class like TacoTruck() has more than one role
Composition vs. Inheritance
By Bri Wyatt
Composition vs. Inheritance
- 150