Skip to content
EntityQ775794· pop 14· linked from 68 articles

Also known as mix-in

In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".

~24 min read

Article

26 sections
Contents
  • History
  • Definition
  • Advantages
  • Implementations
  • Programming languages that use mixins
  • Examples
  • Common Lisp
  • C++
  • Mixin with CRTP
  • Mixin with annotations
  • D
  • Java
  • JavaScript
  • The Object-Literal and <code>extend</code> Approach
  • Mixin with using <code>Object.assign()</code>
  • The pure function and delegation based Flight-Mixin Approach
  • Python
  • Ruby
  • In other languages
  • Interfaces and traits
  • Scala
  • Rust
  • Swift
  • See also
  • References
  • External links

In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".

Mixins encourage code reuse and can be used to avoid the inheritance ambiguity that multiple inheritance can cause (the "diamond problem"), or to work around lack of support for multiple inheritance in a language. A mixin can also be viewed as an interface with implemented methods. This pattern is an example of enforcing the dependency inversion principle.

Connections

Categories