Valid tive C # item 20: distinguish between implementing interfaces and overriding virtual functions

Source: Internet
Author: User

Valid tive C # item 20: distinguish between implementing interfaces and overriding virtual functions

At first glance, the implementation interface is similar to the virtual method of rewriting. They all provide definitions for a declared member. This is wrong. There is a lot of difference between implementing interfaces and rewriting virtual methods. By default, the declared members in the interface are not virtual. The derived class cannot override the interfaces implemented in the base class.

There is a way for the derived class to modify the implementation of interfaces in the base class. To achieve this, we need to create a hook for the derived class ).

The following is an example. The implementation of a simple interface and a class pair interface is as follows:

Interface imsg
{
Void message ();
}
Public class myclass: imsg
{
Public void message ()
{
Console. writeline ("myclass ");
}
}

The message () method is part of the public interface of the myclass class. We can also access members of the myclass type through the imsg interface. Now, let's make the situations in the derived classes a little more complicated:

Public class myderivedclass: myclass
{
Public new void message ()
{
Console. writeline ("myderivedclass ");
}
}

Note that the new keyword is used to define the existing message () method. The myclass. Message () method is not a virtual method, and the derived class cannot be overwritten. Here, the derived class creates a new message () instead of rewriting the methods in the base class. Therefore, we can use the imsg interface to access the message () method in the base class.

Myderivedclass d = new myderivedclass ();
D. Message (); // The result is "myderivedclass"
Imsg M = D as imsg;
M. Message (); // The result is "myclass"

The interface method is not virtual. When we implement the interface, we declare a specific implementation with a special meaning for our class.

But sometimes we want to create interfaces, implement them in the base class, and modify their behavior in the derived class. This can be done. We have two options. If our base class is inaccessible, we can re-implement this interface in the derived class:

Public class myderivedclass: myclass, imsg
{
Public new void message ()
{
Console. writeline ("myderivedclass ");
}
}

The added imsg changes the behavior of the derived class. Now imsg. Message () uses the version implemented in the derived class.

Myderivedclass d = new myderivedclass ();
D. Message (); // The result is "myderivedclass"
Imsg M = D as imsg;
M. Message (); // The result is "myderivedclass"

We still need to use the new keyword to declare the myderivedclass. Message () method. However, this problem still exists. By referencing the base class, we will still get the implementation version in the base class.

The only way to solve this problem is to modify the base class and declare the interface method as virtual.

Public class myclass: imsg
{
Public Virtual void message ()
{
Console. writeline ("myclass ");
}
}

In this way, all classes derived from myclass can declare their own message. Whether using a derived class, an interface, or a base class, the called version is always a rewritten version.

If we do not like this messy virtual function, we can make some modifications to the base class:

Public abstract class myclass: imsg
{
Public Virtual void message ();
}

Now we can implement an interface without fully implementing the method. Because the method declaration is abstract, we must provide its specific implementation in the derived class. Imsg is part of the myclass declaration, but the specific definition of the method is transferred to the implementation in the derived class.

There are more options to implement interfaces than to create and override virtual methods. We can declare the implementation as sealed, virtual, or abstract. We can decide whether the derived class can modify the default Implementation of interfaces in the base class based on the actual situation.

Translated from Objective C #: 50 specific ways to improve your C # by Bill Wagner

Back to directory
 

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.