Thursday, October 18, 2007

Abstract versus Interface

An article on the MSDN describes a summary of both the definitions. Although the article is a bit old (2003) and it compares C# with C++ it still provides a good start.

On the Code Project I found a nice article written by Rahman Mahmoodi where her has a nice comparisson for these two definitions. Another article on the Code Project which seems even more in dept about abstract class is written by Jayababu.

Some important points about Abstract classes

· In contrary to other object oriented languages like JAVA, you can not inherit from more than one abstract class

· you can not instanciate them or you get a Compiler Error

· An abstract class may have a non-abstract constructor implementation.

· they might have (also) non-abstract methods having implementation. these non-abstract methods are callable from the

abstract class AbstractClass
public void NonAbstractMethod()
You can call a non-abstract method which is implemented within the abstract method.");

· You can reimplement the non-abstract method but it would be called by the class internally or any object of that type. If you define an object of abstract type and you instanciate the derived class you still call the implementation within the abstract class. To get the reimplementation you need to instanciate the class using a reference of type of the derived class. For example defining the following in the derived class:

public new void NonAbstractMethod()
This is new implementation of the non-abstract method.");

and calling it like this

// Will call the original implementation
AbstractClass obj = new InheritedClass();

// Will call the re-implementation
InheritedClass obj2 = new InheritedClass();

· In your derrived class you need to ovreride all the abstract methods.

public override void OverrideThis()
You have to implement an abstract method using the override key");

· An abstract method can not be marked as static, compile error.

· You can have static and non-static fields in an abstract class:

public static int count = 1;
public int InstCount = 1;


but when instanciating two objects of the derived class and verify the value of the fields:

Console.WriteLine("Static field is " + AbstractClass.count);
Instance counter is " + obj1.InstCount);
Instance counter is " + obj2.InstCount);


Static field is 3
Instance counter is 2
Instance counter is 2

The Differences

· An inheritted class cannot have multiple base classes: compiler error.

· An abstract class can define a default implementation (using a non-abstract method) and that will not force the derived classes to reimplement it whereas an interface can not have any implementation at all.

· Interfaces can not contain fields; compile error.

· An interface is like a contract. Once the contract changes all the classes implementing it need to change whereas an abstract class is like a common code for all the classeds and you might introduce the new method as a non-abstract method and give a default implementation. This could be seen as a pro or con. In some situations it is required to force all the contract holders to apply on the new method and in other cases you want to have this new feature for some of the types while the rest migh follow later. (a good question would be why is the new method part of the abstract class and not introduced as a new contract)


1 comment:

Anonymous said...

Greate pieces. Keep posting such kind of info on your site.
Im really impressed by your blog.
Thanks for sharing your thoughts about c# or. Regards
my web page: C#