difference between abstract and interface

Posted by Shanky11 under ASP.NET on 11/27/2012 | Points: 10 | Views : 1105 | Status : [Member] | Replies : 3
where to use abstract and interface in .net.please provide the deetails


Posted by: Saratvaddilli on: 11/27/2012 [Member] [MVP] Bronze | Points: 25

Abstract and Interface:
we often confuse whats the difference between Abstract classes and Interfaces both act like a templates
In Abstract :
we can declare a method as well as some variables
In Interfaces:
we can only declare methods

for more in depth go through this link

Thanks and Regards
Show difficulties that how difficult you are

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 11/27/2012 [Member] [MVP] Gold | Points: 25

Use an abstract class
-When creating a class library which will be widely distributed or reused-especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
-Use an abstract class to define a common base class for a family of types.
-Use an abstract class to provide default behavior.
-Subclass only a base class in a hierarchy to which the class logically belongs.

Use an interface
-When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
-Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance-allowing a specific type to support numerous behaviors.
-Use an interface to design a polymorphic hierarchy for value types.
-Use an interface when an immutable contract is really intended.
-A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

Refer this url

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

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.

An abstract class is a special kind of class that cannot be instantiated. It normally contains one or more abstract methods or abstract properties. It provides body to a class.


An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.

It's an abstract class with public abstract methods all of which must be implemented in the inherited classes.

In which Scenario you will go for Interface or Abstract 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. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published.
Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

Difference Abstract class vs Interface

Abstract Class:-
1) An abstract method is created by specifying the abstract type modifier.
2) An abstract method contains no body.
3) An abstract method is not implemented by the base class.
4) An abstract method is automatically virtual.
5) A derived class must override it.
6) Abstract class can have modifiers for methods,properties etc.,
7) An abstract class can implement a property.
8) The abstract modifier cannot be applied to static methods.
9) Properties can also be abstract.
10) A class containing abstract methods must be declared as abstract with the abstract specifier.
11) There can be no objects of an abstract class.
12) If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.
13) An abstract class can inherit from a class and one or more interfaces.
14) An abstract class can implement code with non-Abstract methods.
15) Abstract class can have constant and fields.
16) An abstract class can have constructors or destructor's.
17) An abstract class cannot be inherited from by structures.
18) An abstract class cannot support multiple inheritance.
19) 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.

1) Interfaces cannot be instantiated directly.
2) Interfaces can contain events, method, indexer and properties.
3) An Interface can contain property definitions.
4) Interfaces contain no implementation of methods.
5) Classes and Structs can implement more than one interface.
6) An Interface can be inherited from by structures.
7) An interface itself can inherit from multiple interfaces (Interface can support multiple
8) An abstract class can implement a property.
9) 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 this post helps you mark it as answer

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response