C# .NET - what is difference between abstract class and interface class

Asked By prasanna venaktesan on 28-Aug-08 12:19 AM
end of post

Difference between abstract class and interface - Shailendrasinh Parmar replied to prasanna venaktesan on 28-Aug-08 12:29 AM

See these articles for difference between abstract class and interface ::

http://www.geekinterview.com/question_details/15817

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

Hope it helps.

See this - Sagar P replied to prasanna venaktesan on 28-Aug-08 12:36 AM

Abstract Class..
1.It cannot defines all the methods
2.It has subclass.
3.Here, Subclass is useless
4.A class can be extend an abstract class

Interface..
1.It defines all the methods
2.It must have implementations by other classes, But there will be no use of that.
3.Only an interface can extend another interface.

For More details..

http://kyapoocha.com/c-sharp-interview-questions/what%E2%80%99s-the-difference-between-an-interface-and-abstract-class-5/

http://www.dotnetuncle.com/Difference/4_abstract_class_interface.aspx

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

Best Luck!!!!!!!!!!!!!!!!!!!!!!!
Sujit.

abstract class and interface class - pravin kumar S replied to prasanna venaktesan on 28-Aug-08 12:39 AM

An abstract class and Interface both have method only but not have body of method.The difference between Abstract class and An Interface is that if u call Ablstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not necessary that u call all method of that particular interface.Method OverLoading:-Return type, Parameter type, parameter and body of method number may be different.Method Overriding:- Return type, Parameter type, Parameter Number all must be same . Only body of method can change
diff - Web Star replied to prasanna venaktesan on 28-Aug-08 12:42 AM
1) Abstract class defines few or none of the methods, but 
interface defines all the methods..
2) Abstract classes should have subclasses else that will 
be useless..
Interfaces must have implementations by other classes else 
that will be useless
3)only an interface can extend another interface, but any 
class can extend an abstract class..
4)All variable in interfaces are final by default
5) Accessibility modifier(Public/Private/internal) is 
allowed for abstract class.
Interface doesn't allow accessibility modifier
6) class can inherit only one abstract class
class can implement more than one interface.
Interface: 1)In case if interface it is one contract.The 
functions which we define functions we have to make 
implements in body.
            2)It can come in inheritance chain
Abstract Class:1) In case of Abstract class we have to use 
keyword that is"Mustinherit".
               2)In that this is not come in inheritance 
chain
See this - Kalit Sikka replied to prasanna venaktesan on 28-Aug-08 12:44 AM

Feature

Interface

Abstract class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface cannot provide any code, just the signature.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Requires more time to find the actual method in the corresponding classes.

Fast

Adding functionality (Versioning)

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants

No fields can be defined in interfaces

An abstract class can have fields and constrants defined

do check here - Santhosh N replied to prasanna venaktesan on 28-Aug-08 12:56 AM

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards

Please check here for detailes explaination on the differences including sample code..

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

Issue... - Atul Shinde replied to prasanna venaktesan on 28-Aug-08 01:26 AM

Abstract Class:

1)   We can not instantiate the abstract class.

2)   Abstract class works like a base class.

3)   Abstract class must be inherited. (It comes in inheritance chain.)

4)   Abstract class can have abstract methods and discrete methods.

5)   Abstract methods must be implemented in derived class.

Interface:

1)   We must write prototype (methods declaration) in the interface.

2)   We need to implement the interface we can not inherit interface.

3)   Interface can be implemented to many classes.

4)   We can achieve multiple inheritance (technique) with the help of interfaces.

reply - Binny ch replied to prasanna venaktesan on 28-Aug-08 01:44 AM
You cannot create an object of abstract class , but can make derivations of this.
An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.

An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

Defining an abstract class with all the abstract members is similar to defining an interface. i.e we can say an interface is an abstract class with all the abstract members


Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.

In short Interface is a abstract class having all methods abstract.

Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.

If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.

If you use a virtual keyword in a method then you can override this method in the subclass if you wish..

If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.



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

http://msdn.microsoft.com/msdnmag/issues/03/12/CQA/
reply - Binny ch replied to prasanna venaktesan on 28-Aug-08 01:51 AM
Abstract class:
A class that cannot be instantiated.  An abstract class is a class that must be inherited and have the methods overridden.  An abstract class is essentially a blueprint for a class without any implementation.

Interface class?
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.

Difference between an interface and abstract class?
In an interface class, all methods are abstract - there is no implementation.  In an abstract class some methods can be concrete.  In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.

Go through thse links:
http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx
 Its has the programaticall explainations