Could someone please explain to me the differences between abstract classes, interfaces, and mixins? I've used each before in my code but I don't know the technical differences.
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Name for a method that has only side effects
相关文章
- List可以存储接口类型的数据吗?
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- NameError: name 'self' is not defined, eve
- Implementation Strategies for Object Orientation
- How to override Bootstrap mixin without modifying
- Alternatives to abstract classes in Ruby?
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
Abstract Class
An abstract class is a class that is not designed to be instantiated. Abstract classes can have no implementation, some implementation, or all implementation. Abstract classes are designed to allow its subclasses share a common (default) implementation. A (pseudocoded) example of an abstract class would be something like this
A subclass might look like
Possible usage
If a subclass does not override unimplemented methods, it is also an abstract class.
Interface
In general computer science terms, an interface is the parts of a program exposed to a client. Public classes and members are examples of interfaces.
Java and C# have a special
interface
keyword. These are more or less an abstract class with no implementation. (There's trickiness about constants, nested classes, explicit implementation, and access modifiers that I'm not going to get into.) Though the part about "no implementation" doesn't fit any more in Java, they added default methods. Theinterface
keyword can be seen as a reification of the interface concept.Going back to the Shape example
Java and C# do not allow multiple inheritance of classes with implementation, but they do allow multiple interface implementation. Java and C# use interfaces as a workaround to the Deadly Diamond of Death Problem found in languages that allow multiple inheritance (which isn't really that deadly, if properly handled).
Mixin
A mixin (sometimes called a trait) allows multiple inheritance of abstract classes. Mixins don't have the scary association that multiple inheritance has (due to C++ craziness), so people are more comfortable using them. They have the same exact Deadly Diamond of Death Problem, but languages that support them have more elegant ways of mitigating it than C++ has, so they're perceived as better.
Mixins are hailed as interfaces with behavioral reuse, more flexible interfaces, and more powerful interfaces. You will notice all these have the term
interface
in them, referring to the Java and C# keyword. Mixins are not interfaces. They are multiple inheritance. With a prettier name.This is not to say that mixins are bad. Multiple inheritance isn't bad. The way C++ resolves multiple inheritance is what everyone gets all worked up about.
On to the tired, old Shape example
You will notice there is no difference between this and the abstract class example.
One extra tidbit is that C# has supported mixins since version 3.0. You can do it with extension methods on interfaces. Here's the Shape example with real(!) C# code mixin style
Basically an abstract class is an interface with some concrete implementation. An interface is just a contract that has no implementation detail.
You would use and abstract class if you want to create common functionality amoung all of the objects that implement the abstract class. Keeping with the DRY (Don't Repeat Yourself) principle of OOP.
The meaning of 'Mixin' is excellently defined by Joshua Bloch in his effective Java book. An excerpt from the same book:
"mixin is a type that a class can implement in addition to its “primary type” to declare that it provides some optional behavior. For example, Comparable is a mixin interface that allows a class to declare that its instances are ordered with respect to other mutually comparable objects. Such an interface is called a mixin because it allows the optional functionality to be “mixed in” to the type’s primary functionality."
Reference to Java and given example of Abstract class to provide mixin is misleading. First of all, Java does not support "mixins" by default. In Java terms abstract class and Mixins become confusing.
A mixin is a type that a class can implement in addition to its "primary type" to indicate that it provides some optional behavior. To speak in Java terms, one example would be your business value object implementing Serializable.
Josh Bloch says - "Abstract classes can not be used to define mixins - since a class can not have more than one parent" ( Remember Java allows only one "extends" candidate)
Look for languages like Scala and Ruby for appropriate implementation of the notion of "mixin"
An abstract class is a class that not all of its members are implemented ,they are left for the inheritors to be implemented.It forces its inheritors to implement its abstract members. Abstract classes can't be instantiated and thus their constructors shouldn't be public.]
Here's an example in C#:
}
An interface is a contract to be implemented by a class.It just declare the signature of the members of an implementing class and it has no implementation itself.We usually use interfaces to implement polymorphism,and to decouple dependent classes.
Here's an example in C#:
Since many of guys have explained about the definitions and usage, I would like to highlight only important points
Interface:
has a
" capabilities.Abstract class:
Share code among several closely related classes. It establishes "
is a
" relation.Share common state among related classes ( state can be modified in concrete classes)
I am closing the difference with a small example.
Animal
can be an abstract class.Cat
andDog
, extending this abstract class establishes "is a
" relation.Cat
is a
AnimalDog
is a
Animal.Dog
can
implementBark
interface. Then Doghas a
capability of Barking.Cat
can
implementHunt
interface. Then Cathas a
capability of Hunting.Man, who is
not Animal
, can implementHunt
interface. Then Manhas a
capability of Hunting.Man and Animal (Cat/Dog) are unrelated. But Hunt interface can provide same capability to unrelated entities.
Mixin:
abstract class
andinterface
. Especially useful when you want to force a new contract on many unrelated classes where some of them have to re-define new behaviour and some of them should stick to common implementation. Add common implementation in Mixin and allow other classes to re-define the contract methods if neededIf I want to declare an abstract class, I will follow one of these two approaches.
Move all abstract methods to
interface
and my abstract class implements that interface.Related SE question :
What is the difference between an interface and abstract class?