Monday, 20 February 2012

Difference between Abstract class and Interface in C# .Net

What is an Abstract Class?

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.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

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 twoclasses: 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 abstractclass are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base classdefinition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Abstract class
Multiple inheritance
class may inherit several interfaces.
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 IMovableinterface.
An abstract class defines the core identity of aclass and there it is used for objects of the same type.
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 abstractclass is better to use.
Requires more time to find the actual method in the corresponding classes.
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of theinterface 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 ininterfaces
An abstract class can have fields and constrants defined

Following are features of a abstract class:-

• Object of abstract class cannot be created
• Abstract class is designed to act as a base class (to be inherited by other classes).Abstract class is a design concept in program development and provides a
base upon which other classes are built.
• Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
• In VB.NET, abstract classes are created using “MustInherit” keyword.In C# we have “Abstract” keyword.
• Abstract classes can have implementation or pure abstract methods, which should be implemented in the child class.

Following are the differences between abstract and interfaces:-

• Abstract classes can have concrete methods while interfaces have no methods implemented.
• Interfaces do not come in inheriting chain, while abstract classes come in inheritance.

What is an Interface?
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
• Single Class can implement multiple interfaces.
• If a class implements a interface then it has to provide implementation to all its methods.
An Interface cannot implement methods.
An abstract class can implement methods. 

An Interface can only inherit from another Interface.
An abstract class can inherit from a class and one or more interfaces. 

An Interface cannot contain fields.
An abstract class can contain fields. 

An Interface can contain property definitions.
An abstract class can implement a property. 

An Interface cannot contain constructors or destructors.
An abstract class can contain constructors or destructors. 

An Interface can be inherited from by structures.
An abstract class cannot be inherited from by structures. 

An Interface can support multiple inheritance.
An abstract class cannot support multiple inheritance. 

No comments:

Post a Comment