Professional Documents
Culture Documents
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: Person
{
int iWeight;
public Man()
{}
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.
public Man()
{}
override public void Eat()
{ MessageBox.Show("Man:Eat"); }
override public int Weight
{ set
{ iWeight = value; }
get
{ return iWeight; }
}
You get the following result when you execute the above code.
Man:Eat
Person:Sleep
So why would you declare a class abstract? Its 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.