Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between mixins and inheritance?

Tags:

I am trying to understand the Mixins in the context of scala. In particular I wanted to know difference between concepts of inheritance and Mixins.

The definition of Mixin in wiki says :

A mixin class acts as the parent class, containing the desired functionality. A subclass can then inherit or simply reuse this functionality, but not as a means of specialization. Typically, the mixin will export the desired functionality to a child class, without creating a rigid, single "is a" relationship. Here lies the important difference between the concepts of mixins and inheritance, in that the child class can still inherit all the features of the parent class, but, the semantics about the child "being a kind of" the parent need not be necessarily applied.

In the above definition, I am not able to understand the statements marked in bold. what does it mean that

  1. A subclass can inherit functionality in mixin but not as a means of specialization
  2. In mixins, the child inherits all features of parent class but semantics about the child "being a kind" the parent need not be necessarily applied. - How can a child extend a parent and not necessarily a kind of Parent ? Is there an example like that.
like image 575
Anveshan Avatar asked Apr 20 '16 01:04

Anveshan


People also ask

What is the difference between a mixin and inheritance in Dart?

Mixins are useful when you need code sharing without using inheritance. When you use class B with A {} you are importing every method of mixin A into your class B . Optionally, the usage of a mixin can be constrained to a certain type using the on keyword.

Are mixins composition or inheritance?

Mixins are a form of object composition, where component features get mixed into a composite object so that properties of each mixin become properties of the composite object.

What is the difference between a trait and a mixin?

Traits are compile-time external values (rather than code generated from an external source). The difference is subtle. Mixins add logic, Traits add data such as compile-time type information.

What is mixin based inheritance?

Mixin-based inheritance means that although every class (except for Object) has exactly one superclass, a class body can be reused in multiple class hierarchies.


1 Answers

I'm not sure I understood your question properly, but if I did, you're asking how something can inherit without really meaning the same thing as inheriting.

Mixins, however, aren't inheritance – it's actually more similar to dynamically adding a set of methods into an object. Whereas inheritance says "This thing is a kind of another thing", mixins say, "This object has some traits of this other thing." You can see this in the keyword used to declare mixins: trait.

To blatantly steal an example from the Scala homepage:

abstract class Spacecraft {   def engage(): Unit } trait CommandoBridge extends Spacecraft {   def engage(): Unit = {     for (_ <- 1 to 3)       speedUp()   }   def speedUp(): Unit } trait PulseEngine extends Spacecraft {   val maxPulse: Int   var currentPulse: Int = 0   def speedUp(): Unit = {     if (currentPulse < maxPulse)       currentPulse += 1   } } class StarCruiser extends Spacecraft                      with CommandoBridge                      with PulseEngine {   val maxPulse = 200 } 

In this case, the StarCruiser isn't a CommandoBridge or PulseEngine; it has them, though, and gains the methods defined in those traits. It is a Spacecraft, as you can see because it inherits from that class.

It's worth mentioning that when a trait extends a class, if you want to make something with that trait, it has to extend that class. For example, if I had a class Dog, I couldn't have a Dog with PulseEngine unless Dog extended Spacecraft. In that way, it's not quite like adding methods; however, it's still similar.

like image 94
Nic Avatar answered Oct 22 '22 12:10

Nic