views:

626

answers:

4

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. (And yes, I've searched, but everything I found was either too technical or otherwise unhelpful.)

+3  A: 

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.

Jon Erickson
There's more to it than that. For instance, an abstract class can define abstract protected method, an interface can not. And you can implement any number of interfaces but you can only extend one abstract class (unless your language has multiple inheritance).
n3rd
yes there is definitely more to it then what I have described (and to @musicfreak, you should research more because there is a lot on this topic). just trying to give a quick definition for the OP since he stated that he/she wanted something less technical.
Jon Erickson
+1 for keeping it simple and to-the-point. Also, n3rd, thanks for the info.
musicfreak
+9  A: 

In general:

An interface is a contract specifying operations, but without any implementation. Some languages (Java, C#) have baked-in support for interfaces, and in others 'interface' describes a convention like a pure virtual class in C++.

An abstract class is a class which specifies at least one operation without an implementation. Abstract classes can also provide some parts of their implementation. Again, some languages have baked-in support for marking classes as abstract and it is implicit in others. For example, in C++ a class which defines a pure virtual method is abstract.

A mixin is a class which is designed to make implementation of certain functionality easier in subclasses, but which is not designed to be used by itself. For example, say that we have an interface for an object that handles requests

interface RequestHandler {
  void handleRequest(Request request);
}

Perhaps it would be useful to buffer the requests by accumulating them until we have some predetermined number and then flushing the buffer. We can implement the buffering functionality with a mixin without specifying the flush behavior:

abstract class BufferedRequestHandlerMixin implements RequestHandler {
  List<Request> buffer = new List<Request>();

  void handleRequest(Request request) {
    buffer.add(request);

    if (buffer.size == BUFFER_FLUSH_SIZE) {
        flushBuffer(buffer);
        buffer.clear();
    }
  }

  abstract void flushBuffer(List<Request> buffer);
}

This way, it's easy for us to write a request handler that writes requests to disk, calls a web service, etc. without rewriting the buffering functionality each time. These request handlers can simply extend BufferedRequestHandlerMixin and implement flushBuffer.

Another good example of a mixin is one of the many support classes in Spring, viz. HibernateDaoSupport.

Paul Morie
Great, this is exactly what I needed, thank you. One thing however: could you clarity exactly how mixins "make implementation of some behavior easier in subclasses"?
musicfreak
Let me know if the example helps.
Paul Morie
Yes, excellent! Thank you!
musicfreak
Wait, maybe i'm missing something but isn't BufferedRequestHandlerMixin an abstract class? How does this differ from a Mixin?
mR_fr0g
mR_fr0g, yes, BufferedRequestHandlerMixin is implemented as an abstract class. It's common to implement mixins using abstract classes in Java, as the abstract keyword notes that the class is designed for reuse and isn't mean to be used by itself (it also obviously prevents you from using it by itself).
Paul Morie
"An abstract class is a class which specifies at least one operation without an implementation" : not quite true for .NET. For example the following is a valid class definition in c#: "public abstract class Foo { }"
Darin Dimitrov
+1  A: 

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#:

    public abstract class Employee
    {
        protected Employee(){} 
        public abstract double CalculateSalary(WorkingInfo workingInfo);//no implementation each type of employee should define its salary calculation method.
    }

   public class PartTimeEmployee:Employee
  {
    private double _workingRate;
    public Employee(double workingRate)
    {
     _workingRate=workingRate;
    }
    public override double CalculateSalary(WorkingInfo workingInfo)
    {
      return workingInfo.Hours*_workingRate;
    }

}

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#:

public interface IShape
{
int X{get;}
int Y{get;}
void Draw();
}

public class Circle:IShape
{
public int X{get;set;}
public int Y{get;set;}

public void Draw()
{
//Draw a circle
}
}

public class Rectangle:IShape
{
public int X{get;set;}
public int Y{get;set;}

public void Draw()
{
//Draw a rectangle
}
}
Beatles1692
+1 for the examples. Thanks.
musicfreak
you're welcome!
Beatles1692
+1  A: 

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"