views:

108

answers:

4

Possible Duplicate:
Interface vs Base class

Hello all,

I am not understanding difference between abstract class and interface, could you tell me please when I need to use which art of type.

Thanks a lot and take care, Ragims

A: 

An abstract class is class probably with some abstract methods and some non-abstract methods. They do stuff (have associated code). If a new non-abstract class, subclasses the abstract class it must implement the abstract methods.

I.E.

public abstract class A {
    public string sayHi() { return "hi"; } // a method with code in it
    public abstract string sayHello();  // no implementation
}

public class B 
   : A
{
    // must implement, since it is not abstract
    public override string sayHello() { return "Hello from B"; }
}

Interface is more like a protocol. A list of methods that a class implementing that interface must have. But they don't do anything. They have just method prototypes.

public interface A
{
    string sayHi(); // no implementation (code) allowed
    string sayHello();  // no implementation (code) allowed
}

public class B
    : A
{
     // must implement both methods
    string sayHi() { return "hi"; }
    string sayHello() { return "hello"; }
}

Both are often confused because there is no protocol/interface in C++. So the way to simulate an interface behavior in that language is writing a pure virtual class (a class with only pure virtual functions).

class A {
    virtual int a() = 0;  // pure virtual function (no implementation)
} 
Pablo Santa Cruz
+6  A: 

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

article along with the demo project discussed Interfaces versus Abstract classes.

Pranay Rana
A: 

A quick google search of your question produced the following results:

http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/8ad621b8-a915-4d7e-89c3-5dbbc47202fd

http://sadi02.wordpress.com/2008/05/08/what-is-difference-in-an-abstract-class-and-an-interface/

LOTS and LOTS of stuff out there on google about this.

Ian

Ian P
i would like to hear it also here, what are people meaning.
Ragims
+1  A: 

Try thinking of it like this:

An abstract class creates an "is-a" relationship. Volkswagon is a Car.

An interface creates a "can-do" relationship. Fred can IDrive.

Moreover, Fred can IDrive, but Fred is a Person.

Ragoczy