Abstract Class versus Interface

By: Jimit Shah | Views: 3051 | Date: 20-Jun-2010

he concept of Abstract classes and Interfaces is a bit confusing for beginners of Object Oriented programming. Therefore, I am trying to discuss the theoretical aspects of both the concepts and compare their usage.

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.

Both Together

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.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

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 ModfiersAn interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as publicAn 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 ConstantsNo fields can be defined in interfacesAn abstract class can have fields and constrants defined

Previous Page Next Page

People Searching On This Page:
  • classes versus interfaces in .NET
  • interface versus class
  • main differance between abstract class and interface in c#.net
  • difference between abstract class and interface
  • abstract class versus interface
  • interface vs abstract c#
  • interface versus abstract class .net c#
  • why interface used in c net
  • difference between abstract class and interface in csharp
  • interface in c .net
  • why we need abstract class in .net
  • C# Abstract Class versus Interface
  • multiple interface in c sharp
  • abstract class and interface in c .net c#
  • c sharp local abstract class

Related Pages


I.C.S.E. Class XII (12th Class) Exam Result 2013

I.C.S.E. Class XII (12th Class) Exam Result 2013

Articles | Exams | Results
Date:
08-Feb-2013  Views: 2966

Council for the Indian School Certificate Examination is going to declare I.C.S.E. Class XII (12th Class) Exam Result 2013. Read full article and subs ...
I.C.S.E. Class X (10th Class) Exam Result 2013

I.C.S.E. Class X (10th Class) Exam Result 2013

Articles | Exams | Results
Date:
08-Feb-2013  Views: 3591

Council for the Indian School Certificate Examination is going to declare I.C.S.E. Class X (10th Class) Exam Result 2013. Read full article and subscr ...
CBSE Class XII (Class 12th) Exam Result 2013

CBSE Class XII (Class 12th) Exam Result 2013

Articles | Exams | Results
Date:
08-Feb-2013  Views: 2716

Central Board of Secondary Education is going to declare CBSE Class XII (Class 12th) Exam Result 2013. Read full article and subscribe with us to get ...
CBSE Class X (Class 10th) Exam Result 2013

CBSE Class X (Class 10th) Exam Result 2013

Articles | Exams | Results
Date:
08-Feb-2013  Views: 3948

Central Board of Secondary Education is going to declare CBSE Class X (Class 10th) Exam Result 2013. Read full article and subscribe with us to get up ...
Understanding C# Abstract Classes

Understanding C# Abstract Classes

Articles | Programming | C Sharp.NET / C#.NET
Date:
20-Jun-2010  Views: 2185

we have created classes which could be both instantiated as objects and used as a base class from which to derive classes. Often a base class is not i ...
Post Your Comments (No Login Require)
Name : (required)
Email : (required)
Website :

Comment : (required)

16  + 8 =     
Comments
Bala Sakthis  |  13-Jul-2010 10:56:19 IST
Hi Jimit! Nice article and the comparison tabular column was very good. Keep posting such articles.

Thanks!
Bala
Jimit Shah  |  13-Jul-2010 12:07:54 IST
Thanks Bala for Appreciating...
People Searched About:
Classes Versus Interfaces In .NET   |   Interface Versus Class   |   Main Differance Between Abstract Class And Interface In C#.Net   |   Difference Between Abstract Class And Interface   |   Abstract Class Versus Interface   |   Interface Vs Abstract C#   |   Interface Versus Abstract Class .Net C#   |   Why Interface Used In C Net   |   Difference Between Abstract Class And Interface In Csharp   |   Interface In C .Net   |   Why We Need Abstract Class In .Net   |   C# Abstract Class Versus Interface   |   Multiple Interface In C Sharp   |   Programming In C Sharp.Net Book   |   C# Class Versus Interface   |  
Google : 653 times | Yahoo : 151 times | Bing : 453 times |