26

I have to explain to some students the use of abstract classes and interfaces, as I have a very tecnical background, I would like to know if you would help me to define an easy explanation for junior.

Simple definition:

What are the purposes of abstract classes, interfaces, and what is the difference between the two ?

When is it appropriate to use one instead of the other?

Thanks.

3
  • 4
    There are numerous question on SO discussing this topic, Did you have a look at them, Like this and this
    – V4Vendetta
    Sep 8, 2011 at 4:56
  • Thanks I did not know about this two articles, I will have a look. The fact is I need a really very clear and net explanation (even without code) to understand the concept itself.
    – GibboK
    Sep 8, 2011 at 4:58
  • A few useful links for you When to use what Interface Vs Abstract Sep 8, 2011 at 19:46

4 Answers 4

33

I'll see if I can do this with generic terminology without too much hand-waving.

An interface is like a contract. It says that a class which implements the interface agrees to implement all of the functions declared (as signatures only; no function definition) by that interface. The class may do so in any way it chooses, and provide any other functionality, as long as it implements each one of the declared functions. An interface is useful when you want to be able to use some common functionality of otherwise unrelated classes- they share no implementation details, only the function signatures. In C#, function declarations within an interface are implicitly pure virtual.

An abstract class is a partially defined class that cannot be instantiated. It (usually) includes some implementation, but leaves some functions as pure virtual- declared only by their signature. Pure virtual functions are not defined in the class that declares them, so they must be implemented by a subclass (unless it too is an abstract class). Only a subclass which defines all of the pure virtual functions can be instantiated. The purpose of an abstract class is to define some common behavior that can be inherited by multiple subclasses, without implementing the entire class. In C#, the abstract keyword designates both an abstract class and a pure virtual method.

In practical terms, the difference between the two is that an interface defines only pure virtual functions, while an abstract class may also include concrete functions, members, or any other aspect of a class.

4
  • Thanks Travis, so.. can I say an Interface describes "Roles" for a class to interpreter? Could make sense this analogy?
    – GibboK
    Sep 8, 2011 at 5:21
  • 1
    @GibboK - I'd say that that analogy wouldn't help most people understand these concepts. But it may help you ... depending on what the word "role" means to you.
    – Stephen C
    Sep 8, 2011 at 8:37
  • It's more like communication (aka "usage") protocole than role. That said, a role define such protocole.
    – Klaim
    Sep 9, 2011 at 9:41
  • 1
    I would add that a class may implement multiple interfaces, but (at least in C#) can only inherit from just one class (abstract or not).
    – Andrew
    Jun 19, 2015 at 19:22
20

An interface says to the user that this class will support these functions.

For example:

public interface IWorker
{
    void DoWork();
}
public class MyInformation : IWorker
{
    public void DoWork()
    {
        //Do your class-specific Work 
    }
}

var info = new MyInformation();
info.DoWork();

You might ask here - why not just add the method in MyInformation, instead of implementing an interface?

Here's a situation where it would be useful:

List<IWorker> workers = GetWorkers();
foreach(var worker in workers)
    worker.DoWork();

Here we don't know what kind of objects we're recieving. All we know is that they will implement any of the methods defined in IWorker. We don't care how they do it - all we care about is that they support your call.

As for an abstract class:

public abstract class Serializer
{
    protected void GetAttributes()
    {
        //Some default implementation
    }
    public abstract void SaveObject(String outputPath);
}

We have a serializer class, which has some basic default implementations - but by itself it does nothing. It does not know how to write the output - only how to get the attribute information.

Now you can have a concrete implementation such as :

public class XmlSerializer : Serializer
{
    public override void SaveObject(String outputPath)
    {
        //Do your xml serializing
    }
}

Which will serialize it to XML.

Again, you don't care how they serialize, only that they do it;

List<Serializer> serializers = GetWorkers();
foreach(var serializer in serializers)
    serializer.SaveObject(@"C:\out.tmp");
0
12

Interfaces are like skeletons. If you want to build a human, you should use that skeleton.

Interfaces simply create a kind of structure for your classes to denote that your class should have a defined and agree-upon structure. For example, all animals pee, eat and make sounds. Thus you can have an interface, called IAnimal which only mentions that classes should Pee, Eat, and MakeSound.

Interfaces are just like simple checklists, which should either be implemented totally, or none. When you want to use IAnimal interface, it's just like your boss telling you "Hey, don't create a Cat class unless you write some methods for it to eat, pee, and make sound".

Abstract classes are like skeletons, but with some meat on them as well. It's just there to make your work easier.

You can consider an abstract class to be an interface, which already has some implementation.

That was the plain English answer. But after getting that, please study more, to get the real concept.

1
  • 1
    an absolute explanation @Saeed Dec 17, 2014 at 8:38
3

I will try to be simple.

An abstract class is a class. It is special in that you cannot create object of abstract class. A non-abstract class in called concrete class.

An abstract class, being a class, can have usual elements in it like fields, properties, methods events, indexers etc. Note that, as it is guaranteed that abstract class will not be instantiated to create object, some of those elements cab be without implementations (e.g. methods without body).

An interface is description of members that some other class will have for sure. C# interface can describe methods, properties, indexers and events only.

If a class implements interface, then it must implement all members described by interface.
This means if a class, implementing an interface, does not implement all members described by that interface then such class must be abstract class!

An abstract class can be 'half-cooked' class meaning it has provided few methods with body but few methods are without body etc. An interface can never implement any member it describes.

Abstract class takes part in hierarchy of classes based on inheritance. Note that in C#, a class can have at most one parent class which may or may not be abstract.

A class can implement many interfaces at the same time.

There many use cases for both of them.

Following statements should give you more clarity:

  • Class must be either abstract or concrete.
  • Interface, by definition, is abstract.
  • Neither interface nor abstract class can be instantiated.

  • Class may inherit from zero or one (abstract or concrete) class.

  • Class may implement zero or more interfaces.
  • Interface may inherit from zero or more interfaces.

  • Interface cannot inherit a class.

  • Abstract class may have members with implementation.

  • Interface cannot have any member with implementation.

  • To be concrete, class must implement all members in interface.

I hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.