views:

218

answers:

7
+1  Q: 

Class vs Interface

Recently I was asked in an interview that, can an Interface be considered as a class in C#? I.e. is an interface is a class in C#?

I was confused.

What can be the answer?

+1  A: 

There could be several answers.

No, a class is not an interface - an interface defines a contract, a class is a type of object which can be created.

Yes, an interface can be viewed as a base class with only virtual methods - this is how interfaces are defined in C++.

Michael
+2  A: 

Yes, any abstract class that contains no implementation and consists of abstract methods only would be equivalent to interface.

grigory
Depending on the language it may be nearly equivalent but not identical: e.g in C# a class may implement several interfaces simultaneously but may not subclass more than one abstract class.
ChrisW
Agree. My 'equivalent' doesn't mean neither 'identical' nor 'is'. It simply means that class is equivalent to interface for the set of core interface properties (which do not include multiple inheritance in particular).
grigory
A: 

"Sure, what does this have to do with your current projects?"

rpflo
the fact that if you don't know the difference, you aren't going to be touching the codebase because you'll muck things up and paint us into corners with poor abstraction layer choices. Thanks for coming. Next.
SquareCog
He didn't have C# in the question when this was asked.
rpflo
Not sure what the choice of language has to do with it. Interfaces are a computer science concept, and knowing them is useful even when working in languages without built in support.
Sean Nyman
+2  A: 

A Java interface is not a class; it is a declaration of methods that need to be implemented by classes; a description of abilities, if you will. Abstract classes in Java are an interesting half-way point between proper classes and interfaces, as they define the available methods, but also provide some default implementations.

The fundamental difference between an abstract class and an interface in Java is that you can only extend one class; you can implement multiple interfaces. An abstract class describes what you are; an interface describes what you can do. What you are also defines what you can do -- but it has significantly stronger meaning.

SquareCog
what does Java have to do with c#?
Miky Dinescu
They're similar enough that the concepts hold true - what he says is as relevant to C# as it is to Java.
kyoryu
java and C# are very simmilar
hhafez
I answered before the OP clarified C#. Fortunately for me, C# works the same way :)
SquareCog
+6  A: 

No, an interface is not a class.

An interface is a set of method signatures and possibly properties that all relate to a single idea. For example, the IList interface will have methods for indexing, inserting, and getting the number of elements. However, it does not define any implementation details. The list interface could be implemented as a linked list, or a wrapped up array, or anything you want, as long as it defines those methods in the interface.

A class is the template from which to create an actual object. Classes are a collection of method signatures, plus the implementations of those methods.

So no, an interface is not a class, as it merely defines a specific contract, while a class defines the whole behaviour of the object.

Commenter SquareCog accurately points out that the above is not entirely true. Since classes can be subclassed, and methods overridden, the class' relationship to the actual behaviour of an object gets somewhat more complicated. I'm just going to handwave that problem away by saying that classes are individual entities. You can read the source code for a class and know what behaviour that class encompasses. However, at runtime, objects have types instead of classes. Types are the entire inheritance tree, instead of a singular class, and thus a type's behaviour could be defined over several different classes. Luckily, this does not change the basic conceptual difference that interfaces are contracts that can imply (through names, argument types, etc) certain implementations, but cannot enforce anything but the method signatures, while classes do define an implementation, even if that is not the actual implementation used at runtime.

Sean Nyman
Sean, that's a nicely phrased answer. I would be careful about saying that a class defines behaviour of the object -- it is possible to extend (inherit from) a class and completely rewrite the internals. So just because you know something is a Foo, doesn't necessarily mean you know how it's implemented (it might actually be Bar, the rebellious son of Foo).
SquareCog
@Sean, your avatar don't suit you at all. Plz consider changing it.
JMSA
@SquareCog: true, I didn't consider that. I'll get around it by claiming that classes are individual entities, which each define a specific behaviour, and then an actual object's behaviour is actually defined by its type, which is the amalgamation of it's inheritance tree. That way I don't have to change my post significantly ;P
Sean Nyman
@JMSA: thanks for the advice, but I like my avatar ;). Snakes are cool animals, I've always had a fondness for them.
Sean Nyman
+1  A: 

In general, an Interface is a type that can be implemented by a class to indicate that the class exposes a behavior through a set of methods. For example, .Net has an ICollection interface that contains methods to interact with a collection.


In C++, an interface is a class where every method is abstract.

In Java and .Net, interfaces are independent types that are unrelated to classes.

Either way, classes can implement interfaces.

SLaks
+3  A: 

From a logical perspective, they are very similar. As noted by others, an ABC1 with only public abstract members would serve almost the same purpose as an interface.

When you get down to the nuts and bolts of it, the two have a number of important differences.

  • A class can only inherit from one base classes, but can implement many interfaces.
  • A value type, already deriving from ValueType, cannot inherit from an ABC, but can implement an interface.
  • A class can contain fields and static members. An interface cannot.
  • A class can contain implementation, an interface cannot.
  • A class can have private and protected members, an interface cannot.
  • Abstract members of an ABC are always virtual. A class can implement an interface with non-virtual members.

1: Abstract Base Class

P Daddy