Understanding .NET Interface-Based Programming

Interfaces 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 can’t 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
{
void Method11();
void Method12();
}

public class ImplementorClass : IMyfirstInterface
{
public void Method11()
{...}
public void Method12()
{...}
}

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 method’s 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 interface IMyfirstInterface
{
void Method11();
void Method12();
}

public class ImplementorClass : IMyfirstInterface
{
void IMyfirstInterface .Method11()
{...}
void IMyfirstInterface .Method12()
{...}
}

.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).

| How to Parse an XML Document using DOM | Understanding Code Management in .NET | Understanding Enterprise Transaction Services in .NET | Understanding .NET Interface-Based Programming | Understanding Security Management in Web Services | Understanding SOAP Message and Delivery Structure | Understanding the Basics of Language Integrated Query (LINQ) | Understanding the Basics of Web Services using .NET | Understanding the Basics of XPath |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.