What are the reasons to favour inheritance over mixins
Given the following psuedo-code example :
class Employee
class FullTimeEmployee inherits Employee
class PartTimeEmployee inherits Employee
// versus
class Employee
class WorksPartTime
class WorksFullTime
class FullTimeEmployee includes Employee, WorksFullTime
class PartTimeEmployee includes Employee, WorksPartTime
If we were to use inheritance to build objects the class relations would be seen as a tree where as with mixins the class relations would be seen as a flat list.
Assuming the language we are using
- allows for mixins with a non-verbose syntax
- allows us to treat
FullTimeEmployee
as both aEmployee
andFullTime
object transparently.
why should we build up our class relations as trees (inheritance) instead of flat lists (composition)?
Example of tree versus list.
class Person
class Employee inherits Person
class FullTimeEmployee inherits Employee
// -> FullTimeEmployee
// Person -> Employee
// -> PartTimeEmployee
class Person
class Employee includes Person
class FullTime
class FullTimeEmployee includes FullTime, Employee
//
// FullTimeEmployee = (FullTime, Employee, Person)
//