TechCulture

Icon

A blog on Technology | Culture | Life | People | Experiences

Interfaces and Abstract Classes in .NET

An interface is a contract. When a class implements an interface, it needs to fulfill that contract. Lets take a look at differences between interfaces and abstract classes in C#.

Item Interfaces Abstract Classes
Member declarations Interface members have public access by default. A custom access is illegal. Class members can have access other than public.
Members Interfaces cannot contain any member fields (indexers, events, methods and properties are allowed). An abstract class may contain data members as well.
Method Definition An interface may only contain declarations. An abstract class may also contain definitions or implementations.
Inheritance A struct or a class may implement (or inherit from) multiple interfaces. A (abstract) class may inherit from only one (abstract) class.Also, a struct cannot inherit from a user-defined class (they inherit from System.ValueType).
Inheritance 2 An interface may inherit from multiple interfaces. An (abstract) class may inherit from only one (abstract) class.
Usage Interfaces may be implemented explicitly or implicitly. An abstract class is always inherited from implicitly.
Static An interface may not contain any static methods. An abstract class may contain static methods.
Binding Interface use late binding, but since the methods in an interface are not virtual, the polymorphic behavior is limited to only one level down in the inheritance hierarcy.* An abstract class may define abstract or virtual methods. Their implementations are late bound, and may be overridden in any derived class in the inheritance hierarchy.

* Say I have an interface ITest and 2 classes as follows:

interface ITest
{
void JustDoIt();
}
class Base: ITest
{
public void JustDoIt() {System.Console.WriteLine(“Base”);}
}
class Derived: Base, ITest // Derived class implements ITest
{
public void JustDoIt() {System.Console.WriteLine(“Derived”);}
// Note – Visual Studio warns that: ‘Derived.JustDoIt()’ hides inherited member ‘Base.JustDoIt()’. Use the new keyword if hiding was intended.
}
class Derived2: Base // Derived class does not implement ITest
{
public void JustDoIt() {System.Console.WriteLine(“Derived2”);}
// Note – Visual Studio warns that: ‘Derived2.JustDoIt()’ hides inherited member ‘Base.JustDoIt()’. Use the new keyword if hiding was intended.
}

Derived o = new Derived();
o.JustDoIt(); // prints “Derived”;
((Base)o).JustDoIt(); // prints “Base”
((ITest)o).JustDoIt(); // prints “Derived”

Derived2 o2 = new Derived2();
o2.JustDoIt(); // prints “Derived2”
((Base)o2).JustDoIt(); // prints “Base”
((ITest)o2).JustDoIt(); // prints “Base”

Methods in an interface are not virtual by default. But the implementation CAN make them virtual, and this is allowed. For example, you could have an implementation like:

class MyBase: ITest
{
public virtual void JustDoIt() { System.Console.WriteLine(“MyBase”); }
}

Now you can override JustDoIt in a class that inherits from MyBase and you get no warnings. Additionally you get full polymorphic behavior just like having an abstract class. The distinction between abstract classes and interfaces really starts to get fuzzy here.

In fact, if you could make MyBase abstract, and you don’t have to define even a default implementation for JustDoIt. I don’t know what purpose this serves!🙂

abstract class MyAbstractBase: ITest
{
public abstract void JustDoIt();
}

Filed under: Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: