0

Duplicate:

When to use an interface instead of an abstract class and vice versa?

Probably one of the most famous software developer job interview questions.

What would be your answer?

EDIT: I'm trying to find out how you would answer this in a real-life situation. Please try to formulate your answer as you would on a real job interview (be complete, but don't be too long, post no links of course).

Community
  • 1
  • 1
Chei
  • 2,117
  • 3
  • 20
  • 33
  • I don't want to sound like the members of the local question-closing militia but this has been discussed over and over eleventy billion times here. http://stackoverflow.com/questions/479142/when-to-use-an-interface-instead-of-an-abstract-class-and-vice-versa – Tamas Czinege Mar 25 '09 at 13:33
  • Totally agree; asked many times before. – Mitch Wheat Mar 25 '09 at 13:34
  • Well, this question is about "what's the difference", while that other one is "when would you use one or the other" - that's different enough IMHO to warrant its own question... – DevSolar Mar 25 '09 at 13:34
  • @DevSolar: slight variations of this question appear many times, not just that one quoted... – Mitch Wheat Mar 25 '09 at 13:35
  • http://stackoverflow.com/questions/56867/interface-vs-base-class – Paul Tomblin Mar 25 '09 at 13:35
  • After some searching I still don't think this should have been closed. Lots of Q's about when to use one or the other, but I did *not* find one merely aiming at the syntactical difference. – DevSolar Mar 25 '09 at 13:45
  • "Syntactical difference?" The OP mentions that this is a common interview question. During an interview, the interviewer is probably more interested in design issues than syntax. – Tim Frey Mar 25 '09 at 13:54
  • I should have been more to the point in the original post, I just added my edit. My intent was to try to find the best answer to this interview question. – Chei Mar 25 '09 at 13:57

5 Answers5

7

An interface only describes the actual signature of its methods etc. Any class implementing that interface must then provide an explicit implementation.

An abstract class can contain a partial implementation of its methods etc.

LukeH
  • 263,068
  • 57
  • 365
  • 409
2

An abstract class can have member variables, an interface cannot (or, in C++, should not).

In Java, an "Interface" is a well-defined syntactical element, while in C++ it's merely a design pattern.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • The answer was given as a "answer right now" response. It's not exhaustive, I know, but it's what I came up with ad hoc. – DevSolar Mar 25 '09 at 14:29
2

Interfaces provide definitions of methods that must be implemented by a class. The purpose of interfaces is to allow you to generalise specific functionality regardless of implementation. You may have an IDatabase interface that has an Open/Close method. The class that implements that interface may be connecting to a MySQL database or MS Access database. Irrespective of how it accomplishes this task, the goal is still the same...Open database, close database.

Abstract classes are base classes that contain some abstract methods. They cannot be instantiated they are to be derived from. The purpose of an Abstract class is to allow you to define some generic functionality and sub-class to implement more specific functionality where appropriate.

So in summary, you should use interfaces when the implementation of each class differs completely. Use abstract classes when you have some similar behaviour but need to implement parts differently.

Hope that helps.

James
  • 80,725
  • 18
  • 167
  • 237
0

I would say that the difference is language dependent, but that in C++ at least, abstract classes are the means by which interfaces are implemented.

0

As far as job interviews are concerned, I've always heard that the key point is that an interface is a contract; an interface, while not implementing it itself, guarantees functionality.