views:

3056

answers:

11

Hi,

I have had recently two telephone interviews where I've been asked about the differences between an Interface and an Abstract class. I have explained every aspect of them I could think of, but it seems they are waiting for me to mention something specific, and I dont know what it is.

From my experience I think the following is true, if i am missing a major point please let me know:

Interface:

Every single Method declared in an Interface will have to be implemented in the subclass. Only Events, Delegates, Properties (C#) and Methods can exist in a Interface. A class can implement multiple Interfaces.

Abstract Class Only Abstract methods have to be implemented by the subclass. An Abstract class can have normal methods with implementations. Abstract class can also have class variables beside Events, Delegates, Properties and Methods. A class can only implement one abstract class only due non-existence of Multi-inheritance in C#.

1) After all that the interviewer came up with the question What if you had an Abstract class with only abstract methods, how would that be different from an interface? I didnt know the answer but I think its the inheritance as mentioned above right?

2) An another interviewer asked me what if you had a Public variable inside the interface, how would that be different than in Abstract Class? I insisted you can't have a public variable inside an interface. I didn't know what he wanted to hear but he wasn't satisfied either.

Many Thanks for clarification, Kave

See Also:

+11  A: 

There are a couple of other differences -

Interfaces can't have any concrete implementations. Abstract base classes can. This allows you to provide concrete implementations there. This can allow an abstract base class to actually provide a more rigorous contract, wheras an interface really only describes how a class is used. (The abstract base class can have non-virtual members defining the behavior, which gives more control to the base class author.)

More than one interface can be implemented on a class. A class can only derive from a single abstract base class. This allows for polymorphic hierarchy using interfaces, but not abstract base classes. This also allows for a pseudo-multi-inheritance using interfaces.

Abstract base classes can be modified in v2+ without breaking the API. Changes to interfaces are breaking changes.

[C#/.NET Specific] Interfaces, unlike abstract base classes, can be applied to value types (structs). Structs cannot inherit from abstract base classes. This allows behavioral contracts/usage guidelines to be applied on value types.

Reed Copsey
+1 for the key point that more than one interface can be implemented on a class.
altCognito
That's the one real advantage to interfaces over abstract base classes, IMO. Otherwise, I agree with the .NET design guidelines, that now say to "prefer abstract base classes over interfaces"
Reed Copsey
Although, it would be keen if you could add the point that it's also interfaces can be applied to any class.
altCognito
@altCognito: Figured that was kind of handled with the second paragraph. This did remind me, though, that interfaces work on value types, so I added that.
Reed Copsey
Thank you very much for this exact description. It is indeed very helpful. I am new here. It is a pity you cant select two responses as "answer". One thing that confuses me is your usage of Abstract 'base' class. All abstract classes are meant to be a base class of a subclass. Why naming the 'base' extra?
Kave
@Kave: Habit, for me. I personally differentiate between abstract classes and abstract base classes. You can subclass a concrete class, and add an abstract member to it. This, to me, is subtly different than an "abstract base class", which is suggesting that it's the class at the base of a hierarchy. I suppose this is another difference between abstract classes and interfaces - an abstract class can be a subclass of another (abstract or concrete) class.
Reed Copsey
+3  A: 

For .Net,

Your answer to The second interviewer is also the answer to the first one... Abstract classes can have implementation, AND state, interfaces cannot...

EDIT: On another note, I wouldn't even use the phrase 'subclass' (or the 'inheritance' phrase) to describe classes that are 'defined to implement' an interface. To me, an interface is a definition of a contract that a class must conform to if it has been defined to 'implement' that interface. It does not inherit anything... You have to add everything yourself, explicitly.

Charles Bretana
Yes! State! Thats what the second interviewer meant with his weird way of saying "public variable" inside an interface. gosh! Abstract Classes can have state, interfaces can't!And yeah the everyone else agrees on the differences between their ways of inheritance as well, which I had forgotten to mention but figured out already later. :) Thanks everyone!
Kave
+6  A: 

The interviewers are barking up an odd tree. For languages like C# and Java, there is a difference, but in other languages like C++ there is not. OO theory doesn't differentiate the two, merely the syntax of language.

An abstract class is a class with both implementation and interface (pure virtual methods) that will be inherited. Interfaces generally do not have any implementation but only pure virtual functions.

In C# or Java an abstract class without any implementation differs from an interface only in the syntax used to inherit from it and the fact you can only inherit from one.

Steve Rowe
A: 

From another answer of mine, mostly dealing with when to use one versus the other:

In my experience, interfaces are best used when you have several classes which each need to respond to the same method or methods so that they can be used interchangeably by other code which will be written against those classes' common interface. The best use of an interface is when the protocol is important but the underlying logic may be different for each class. If you would otherwise be duplicating logic, consider abstract classes or standard class inheritance instead.

Adam Alexander
A: 

1) An interface can be seen as a pure Abstract Class, is the same, but despite this, is not the same to implement an interface and inheriting from an abstract class. When you inherit from this pure abstract class you are defining a hierarchy -> inheritance, if you implement the interface you are not, and you can implement as many interfaces as you want, but you can only inherit from one class.

2) You can define a property in an interface, so the class that implements that interface must have that property.

For example:

  public interface IVariable
  {
      string name {get; set;}
  }

The class that implements that interface must have a property like that.

hope this helps!

Regards!!!

MRFerocius
+1  A: 

By implementing interfaces you are achieving composition ("has-a" relationships) instead of inheritance ("is-a" relationships). That is an important principle to remember when it comes to things like design patterns where you need to use interfaces to achieve a composition of behaviors instead of an inheritance.

TheTXI
Interfaces achieve, IMO, more of an "Acts-as-a" relationship. Encapsulation achieves composition better than an interface.
Reed Copsey
+13  A: 

While your question indicates it's for "general OO", it really seems to be focusing on .NET use of these terms.

In .NET (similar for Java):

  • interfaces can have no state or implementation
  • a class that implements an interface must provide an implementation of all the methods of that interface
  • abstract classes may contain state (data members) and/or implementation (methods)
  • abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itslef)
  • interfaces may be multiple-inherited, abstract classes may not (this is probably the key concrete reason for interfaces to exist separately from abtract classes - they permit an implementation of multiple inheritance that removes many of the problems of general MI).

As general OO terms, the differences are not necessarily well-defined. For example, there are C++ programmers who may hold similar rigid definitions (interfaces are a strict subset of abstract classes that cannot contain implementation), while some may say that an abstract class with some default implementations is still an interface or that a non-abstract class can still define an interface.

Indeed, there is a C++ idiom called the Non-Virtual Interface (NVI) where the public methods are non-virtual methods that 'thunk' to private virtual methods:

Michael Burr
Thank you. I think since your answer mentions state + a good overview of all rest, i mark your response as a final answer.You are right I asked for general OO, since my first interviewer asked for general OO, but since I am a C# guy, I tend to forget that. ;-) Also thanks for the C++ explanation, as always c++ is mind blowing.
Kave
I think a key point in the explanation Michael provided is that when implementing an interface you MUST implement all members in the interface, but when inheriting from an abstract class it's NOT REQUIRED by a child class to implement its parent's members
ggomez
A: 

The answer to the second question may be that the public variable defined in interface is static final by default while the public variable in abstract class is an instance variable .

Nidhi Agarwal
A: 

Interfaces are light weight way to enforce a particular behavior. That is one way to think of.

fastcodejava
A: 

Couple of other differences:

Abstract classes can have static methods, properties, fields etc. and operators, interfaces can't. Cast operator allows casting to/from abstract class but don't allow casting to/from interface.

So pretty much you can use abstract class on its own even if it is never implemented (through its static members) and you can't use interface on its own in any way.

Orlin Petrov
A: 

How about an analogy: when I was in the Air Force, I went to pilot training and became a USAF pilot. At that point I wasn't qualified to fly anything, and had to attend aircraft type training. Once I qualified, I was a pilot (Abstract class) and a C-141 pilot (concrete class). At one of my assignments, I was given an additional duty: Safety Officer. Now I was still a pilot and a C-141 pilot, but I also performed Safety Officer duties (I implemented ISafetyOfficer, so to speak). A pilot wasn't required to be a safety officer, other people could have done it as well.

All USAF pilots have to follow certain Air Force-wide regulations, and all C-141 (or F-16, or T-38) pilots 'are' USAF pilots. Anyone can be a safety officer. So, to summarize: Pilot: abstract class C-141 Pilot: concrete class Safety Officer: interface

Jay