What is the difference between a mixin and inheritance?

Technology CommunityCategory: OOPWhat is the difference between a mixin and inheritance?
VietMX Staff asked 11 months ago

mix-in is a base class you can inherit from to provide additional functionality. The name “mix-in” indicates it is intended to be mixed in with other code. As such, the inference is that you would not instantiate the mix-in class on its own. Frequently the mix-in is used with other base classes. Therefore **mixins are a subset, or special case, of inheritance`88.

The advantages of using a mix-in over single inheritance are that you can write code for the functionality one time, and then use the same functionality in multiple different classes. The disadvantage is that you may need to look for that functionality in other places than where it is used, so it is good to mitigate that disadvantage by keeping it close by.