Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an Abstract Class and a Mixin?

I just found an article on a framework in Java that apparently allows it to support Mixins and something called Composite Oriented Programming (which for all I know might even be the same thing...) I've also heard of/worked with AOP, and I'm not sure how it differs from this either...

like image 803
leeand00 Avatar asked Feb 26 '09 16:02

leeand00


People also ask

What is the difference between a mixin and inheritance?

Mixins are sometimes described as being "included" rather than "inherited". In short, the key difference from an inheritance is that mix-ins does NOT need to have a "is-a" relationship like in inheritance. From the implementation point of view, you can think it as an interface with implementations.

What is a mixin used for?

Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like . float-left , and to distribute collections of styles in libraries.

What is a mixin in Java?

Loosely speaking, a mixin is a type that a class can implement in addition to its "primary type" to declare that it provides some optional behaviour. For example Comparable is a mixin interface that allows a class to declare that it its instances are ordered with respect to other mutually comparable objects.

What is difference between interface and abstract class in Dart?

Any class is allowed to implement one or more interfaces. The only difference is that the classes we're using as interfaces don't have the abstract keyword so one() and two() must have a body (it's empty, but it's still there).


1 Answers

At a language-agnostic level, a mixin just adds functionality to a class, and is more for programmer convenience and to avoid code duplication. An abstract (base) class forms an is-a relationship and allows for polymorphism. One reason why inheritance is overused is that it's an easy way to implement mixins without writing any boilerplate in languages that don't really support them. The problem is that you're declaring a polymorphic is-a relationship as a side effect, making your API more confusing and possibly adding ambiguity. Hence, newer languages like D and Ruby support mixins as native features, allowing a convenient way to add a bunch of functionality to a class without declaring a polymorphic is-a relationship.

like image 136
dsimcha Avatar answered Oct 12 '22 14:10

dsimcha