
Understanding .NET Interface-Based ProgrammingInterfaces are constructs having abstract members only. This means that they have the signature or declaration of members only and no functionality implementation is done. The motive behind the use of interfaces is to provide service abstraction i.e. separation of interface from implementation which is a core concept in component based programming. Also with the use of interfaces, service providers i.e. the classes which implement the interface elements are independent to change their implementation details without any interaction with clients as clients and these service providers are connected in loose coupling.
Before discussing
the interface implementation there are some important points about interfaces
you should know like interfaces cant be instantiated and they have
public members only. Also all the interface members must be implemented
by the class which is implementing the interface. An interface
can extend other interfaces and a class can implement multiple interfaces.
Now for understanding the interface implementation by a class consider
the following example: public interface
IMyfirstInterface public class
ImplementorClass : IMyfirstInterface In order
to implement an interface a class is required which should derive it.
Here in the example an interface IMyfirstInterface is defined which has
two methods signatures. For the implementation of this interface
a class named ImplementorClass is defined which has implemented both of
its methods. This method of implementing interface is called implicit
interface implementation. There is
a different technique too where each interface member has to qualify with
the interface name which declares it or whose member it is. This technique
allows the separation of interface from the implementation and known as
explicit interface implementation. Consider the following example to understand
how this is done: public class
ImplementorClass : IMyfirstInterface .NET allows
to mix the both techniques but it should be avoided in order to maintain
consistency. In C# you can not extend more than one class at a time i.e
multiple inheritance is not allowed. To overcome this limitation interfaces
are used as there is no restriction in implementing multiple interfaces,
so in .NET interface also provide a way to maintain scalability and flexibility. One construct
which also defines the signature of members leaving the functionality
implementation is abstract class and it was used as a mechanism to provide
service abstraction in earlier object oriented languages. Though abstracts
classes sounds similar to the interfaces, both of them have some fundamental
differences. A class in
.NET can extend from only one base class, no matter if the base class
is abstract. But as stated earlier it can implement multiple interfaces.
Also abstract classes can have constructors, variables and even non-abstract
methods but interfaces are not allowed to have any of these. Abstract
classes can derive from one base class or multiple interfaces but interfaces
can derive from other interfaces only. These restrictions in interfaces
enable .NET to isolate the definition of implementation (in interfaces)
from the actual implementation code (in the classes implementing the interfaces).
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |