Knowledge Transfer

Thursday, January 18, 2007

Abstract && interface with in C#.Net

Introduction
The choice of whether to use an interface or an abstract can sometimes be a difficult one. This article shows you the differences between these two and also tells you when and where to use them.


Interfaces
Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. An interface represents a contract, and a class that implements an interface must implement every aspect of that interface exactly as it is defined.
You declare an interface by using the interface keyword


Example
interface IPerson
{
void Eat();

void Sleep();

int Weight
{
set;
get;
}
}

In order to use this interface, you must declare your class in the same way that you declare a class inheriting from any other object.


Example
public class Man:IPerson
{
int iWeight;

public Man()
{
}

public void Eat()
{
MessageBox.Show("Man:Eat");
}

public void Sleep()
{
MessageBox.Show("Man:Sleep");
}

public int Weight
{
set
{
iWeight = value;
}
get
{
return iWeight;
}
}

static void Main()
{
Man i = new Man();
i.Eat();
i.Sleep();
}

}


You get the following result when you run the above code.
Man:Eat
Man:Sleep

It is important to note here that an interface is very different from a base class. An interface is implemented, not extended.
1.A class can implement multiple interfaces.
2.An interface cannot contain data declarations but you can declare properties.
3.All method declarations in an interface are public.
4.There can be no implementation in an interface.
5.The class implementing the interface must provide implementation code.
6.An interface can be derived from another interface


Abstract Classes
An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

When you have a class that contains methods that have no implementation (i.e. abstract methods), the class is said to be abstract as well. An abstract method is simply a shell or place-marker for a method that will be defined later in a derived class. The intention of abstract methods is to force every derivation of the class to implement those methods. If a derived class does not implement an abstract method, then that class must also be declared as abstract.

You declare an interface by using the interface keyword
abstract public class Person
{
abstract public void Eat();

public void Sleep()
{
MessageBox.Show("Person:Sleep");
}

abstract public int Weight
{
set;
get;
}
}

You can inherit the abstract class just like any other class.
public class Man:Person
{
int iWeight;

public Man()
{
}
override public void Eat()
{
MessageBox.Show("Man:Eat");
}
override public int Weight
{
set
{
iWeight = value;
}
get
{
return iWeight;
}
}

static void Main()
{
Man i = new Man();
i.Eat();
i.Sleep();
}
}


You get the following result when you execute the above code.
Man:Eat
Person:Sleep

So why would you declare a class abstract? It’s actually a very powerful class hierarchy design tool since you can provide the structure for something that is not very specific —just like our Person class. You will never create an Person object; but you will create Man and Woman objects. The other advantage is that you are moving the code to where it actually belongs. This helps you locate program logic problems.

A class that is derived from an abstract class may still implement interfaces.

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. If additional functionality is needed in derived classes, it can be added to the base class without breaking code.

In the above example, an abstract class is declared with one implemented method, one unimplemented method and one unimplemented property. A class inheriting from this class would have to implement the Eat method and Weight property

When implementing an abstract class, you must implement each abstract method in that class, and each implemented method must receive the same number and type of arguments, and have the same return value, as the method specified in the abstract class.


Recommendations on using Abstract Classes and Interfaces
1. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
3. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
4. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.


Abstract class vs. interfaces
Abstract class Interface
May or may not have abstract methods. Can only have abstract methods (Implicitly abstract) but abstract keyword is not allowed.
Methods can have access modifiers. No access modifier is allowed, implicitly public.
Can have fields. Not intended to define fields.
Can be instantiated via sub-class. Can be instantiated via implementing class.
Can extend a Single class, implement one or more interfaces. Cannot extend a class, Can extend single or multiple interfaces.
Abstract classes form part of the inheritance scheme Interfaces do not. We can use the same interface in two projects that are not related in terms of inheritance. For example an interface IWheel can be implemented in a Car project and also a Bicycle project.
Can have constructor. Can't have constructor.
A class, by comparison, can only extend ("implementation inheritance") one other class Interfaces provide a form of multiple inheritance ("interface inheritance"), because you can implement multiple interfaces.
An abstract class can have static methods, protected parts, and a partial implementation. Interfaces are limited to public methods and constants with no implementation allowed.


if you want some more Stuff on this topic then Click here
Click Here


Labels: , , ,

6 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home