This is a mirror of official site: http://jasper-net.blogspot.com/

Internals of Interface and its Implementation

| Tuesday, October 25, 2011
As many of my followers requested me to write few things that I missed out from the Internals Series, I should continue with it. In this post, I will cover the internals of Interface implementation and mostly talk about explicit interface implementation, as most of the developers seems to be in confusion with it. I hope you will like the post.

Beginning from the basics, Interfaces are the most important part of any application. Interfaces are language construct that does not implement anything but declares a few members upfront. Generally we use interfaces to create a contract between the two or more communication agents. Another important thing that everyone would be knowing already, Interfaces are meant to be implemented. That means whenever you are creating a class, all the members that were there in the interface are meant to be implemented completely. .NET (or probable any other standard language) disallows the creation of objects on types that are not fully defined. Hence abstract classes also coming into play here. They are classes that have few members undefined or abstract. Once you don't have concrete implementation, you cannot create an instance of a type. Notably, you can say "Interface is a types that does not belong to the System.Object or implement it when it reside inside an assembly". But ironically you could also says that once the type is implemented, it would probably inherit from System.object by default.

Another important OOPS feature is that you can hold reference of any concrete type to any of its base implementations. By this what I mean, if say class X derived from Y and implements Z (where Z is an interface) you can say either Y y1 = new X() or Z z1 = new X().

Now lets define an interface and start some tweaks some of its behaviors.

public interface IA
{
    void GetX();
}


Let us suppose we have an interface IA which has a method GetX(). Now you should remember, it is not allowed to use access specifier for members of an interface as that mean the implementers of this interface needs to specify access specifiers for its members and it would appear to all implemtors that these members are public.  Now lets see one implementation of it.

public class A : IA
{
    public void GetX()
    {
        Console.WriteLine("Here is X: Normal");
    }
    void IA.GetX()
    {
        Console.WriteLine("Here is X: Explicitely");
    }
}


Read more: Beyond relational
QR: internals-of-interface-and-its-implementation.aspx

Posted via email from Jasper-net

0 comments: