Implicit and explicit interface implementation

Source: Internet
Author: User

Example 3-1. defining and implementing an Interface

Public interface imyinterface
{
Void Method1 ();
Void method2 ();
D method3 ();
}

Public class myclass: imyinterface
{
Public void Method1 ()
{...}
Public void method2 ()
{...}
Public void method3 ()
{...}
// Other class members
}
 

As trivial as Example 3-1 is, it does demonstrate a number of important points. First, interfaces have visibilityan interface can be private to its assembly (usingInternalAccess modifier) or it can be used from outside the Assembly (withPublicAccess modifier), as in Example 3-1. Second,Even though the methods the interface defines have no access modifiers, they are by definition public,And the implementing class has to declare its interface methods as public.Third, there is no need to useNewOrOverrideTo qualify the method redefinition in the subclass,Because an interface method by its very nature can't have any implementation and therefore has nothing to override. (If you aren't familiar withNewOrOverrideKeywords, see the sidebar "C # inheritance directives" later in this chapter.) Finally,The class must implement all the methods the interface defines, without exception.If the class is an abstract class, it can redefine the methods without providing concrete implementation.

Even if these methods do not have an access modifier, the default value is public.

There is no need to use new or override to determine that the method is redefined in the subclass.

To interact with an object using an interface, all a client has to do is instantiate a concrete class that supports the interface and assign that object to an interface variable, similar to using any other base type. using the same definitions as in Example 3-1, the client code might be:

    IMyInterface obj;    obj = new MyClass(  );    obj.Method1(  );

Interfaces promoteLoose couplingBetween clients and objects. when you use interfaces, there's a level of indirection between the client's code and the object implementing the interface. if the client wasn't responsible for instantiating the object, there is nothing in the client code that pertains to the object hidden behind the interface shield. there can be either possible implementations of the same interface, such:

Interfaces provide loose coupling between customers and objects. When you use an interface, the client code and the class implementing the interface are indirect. If the customer
The end is not responsible for instantiating an object,
    public interface IMyInterface    {...}    public class MyClass : IMyInterface    {...}    public class MyOtherClass : IMyInterface    {...}

When a client obtains an interface reference by creating an object of TypeMyclass, The client is actually saying to. Net "give meMyclass'S interpretation of the wayImyinterfaceShocould be implemented ."

Treating InterfacesAs binary contracts, Which shields clients from changes made to the service providers, is exactly the idea behind com interfaces, and logically ,. net interfaces have the same semantics as com interfaces. if you are an experienced com developer or specified ect, working with interfaces is probably second nature to you, and you will feel right at home. net interfaces.

Using Interfaces as binary contracts (Protocols) hides changes between service providers from customers. This is actually the idea of COM interfaces.

However, unlike COM ,.Net doesn't enforce separation of the interface from the implementation. For example, using the definitions in Example 3-1, the client's code can also be:

However,. Net does not force the separation between interfaces and implementations.

    MyClass obj;    obj = new MyClass(  );    obj.Method1(  );

(Test! This is also acceptable !)

Because of the way the server in Example 3-1 implements the interface (as public members), nothing prevents the client from programming directly against the object providing the service, instead of the interface. I believe this is because. net tries to make component-oriented programming accessible to all developers, including those who have trouble with the more abstract concepts of interface-based programming (see the section ". net adherence to component principles "in Chapter 1 ). the fact that something is possible, of course, doesn't mean you shoshould go ahead and do it. disciplined. NET developers shoshould always enforce the separation, to retain the benefits of interface-based programming.

 

Explicit interface implementation

The way of implementing an interface shown in the previous section is called implicit interface implementation, because a public method with a name and signature that match those of an interface method is implicitly assumed to be an implementation of that interface method.

Example 3-3 demonstrates a simple technique thatAllows server developers to enforce the separation of the interface from the implementation. The server implementing the interface can actually prevent clients from accessing the interface methods directly by using explicit interface implementation.Implementing an interface explicitly means qualifying each interface member name with the name of the interface that defines it.

This method allows server developers to forcibly separate interfaces from implementations. The server can prevent customers from using (explicitly implemented) methods to access interfaces.

This requires that the interface name be used before each member function.

Example 3-3. explicitly implementing an Interface
    public interface IMyInterface    {       void Method1(  );       void Method2(  );    }    public class MyClass : IMyInterface    {       void IMyInterface.Method1(  )       {...}        void IMyInterface.Method2(  )       {...}       //Other methods and members    }

Note thatThe interface members must be implicitly defined as private at the class's scope; you can't use any explicit access modifiers on them, includingPrivate.The only way clients can invoke the methods of explicitly Implemented interfaces is by accessing themVia the interface:

Interface members within the class range must be implicitly defined as private. You cannot explicitly use any access modifiers, including private

The interface is implemented explicitly. the only method that the customer calls the interface member is through the interface.

    IMyInterface obj;    obj = new MyClass(  );    obj.Method1(  );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.