Objective C # Reading Notes -- entry 23: differences between interface methods and Virtual Methods

Source: Internet
Author: User

The difference between implementation and overwriting of Virtual Methods: The members declared in the interface are not virtual methods by default. A derived class cannot overwrite the interface members implemented in the base class. The interface can be explicitly implemented, which hides the public members of the class. Interfaces and virtual methods have different concepts and usage.

 

Changes the behavior of interfaces inherited from the base class in the derived class.

Let's take a simple example:

 1           Interface  Imsg  2   {  3               Void  Message ();  4   }  5           Public   Class Myclass: imsg  6   {  7               Public   Void  Message ()  8   {  9 Console. writeline ( "  Myclass  "  );  10   } 11   }  12   13           Public   Class  Myderivedclass: myclass  14   {  15               Public   New   Void  Message ()  16   {  17 Console. writeline ( "  Myderivedclass  "  );  18   }  19   }  20   21           Public   Class  Effectivecsharp  22   {  23              Public   Static   Void Main ( String  [] ARGs)  24   {  25 Myderivedclass d = New  Myderivedclass ();  26   D. Message ();  27 Imsg M = d As  Imsg; 28   M. Message ();  29   30   Console. Read ();  31   }  32 }

Running output:

We found that after the myderivedclass instance is converted, the message () method is called to change to the message () method of the base class of this class-sometimes we often need to create interfaces, then implement them in the base class and change their implementation in the derived class. What should we do at this time? At this time, there are two ways to choose.

1. Define the interface member implemented in the base class of the implemented interface as virtual and override in the derived class.
 1           Interface  Imsg  2  {  3               Void  Message ();  4   }  5           Public   Class  Myclass: imsg  6   {  7               Public   Virtual   Void  Message () 8   {  9 Console. writeline ( "  Myclass  "  );  10   }  11   }  12   13           Public   Class  Myderivedclass: myclass 14   {  15               Public   Override   Void  Message ()  16   {  17 Console. writeline ( "  Myderivedclass  "  );  18   } 19   }  20   21           Public   Class  Effectivecsharp  22   {  23               Public   Static   Void Main ( String  [] ARGs)  24   { 25 Myderivedclass d = New  Myderivedclass ();  26   D. Message ();  27 Imsg M = d As  Imsg;  28   M. Message ();  29   30   Console. Read ();  31  }  32 }

Running output:

 

2. Define the base class of the implemented interface as an abstract class, and define the implemented interface members as abstract members.

At the same time, we can define the override method of the derived class as a sealed method to prevent its derived class from rewriting the method:

 1           Interface  Imsg  2   {  3               Void  Message ();  4   }  5           Public  Abstract   Class  Myclass: imsg  6   {  7               Public   Abstract   Void  Message ();  8   }  9   10           Public   Class  Myderivedclass: myclass 11   {  12               Public   Sealed   Override   Void  Message ()  13   {  14 Console. writeline ( "  Myderivedclass  "  );  15  }  16   }  17   18           Public   Class  Effectivecsharp  19   {  20               Public   Static   Void Main ( String  [] ARGs)  21  {  22 Myderivedclass d = New  Myderivedclass ();  23   D. Message ();  24 Imsg M = d As  Imsg;  25   M. Message ();  26 Myclass c = (Myclass) m;  27  C. Message ();  28   Console. Read ();  29   }  30 }

Running output:

 

A derived class inherits the implementation of interfaces in the base class.

In fact, the derived class can implement the interface from the base class relay, because the derived class can declare the interface as part of its contract, even if it does not implement any member implementation in this interface, as long as a public and accessible method of the class matches the signature of the interface, the condition of the contract can be met, however, this method cannot be implemented using the display interface. For example:

 1          Interface  Imsg  2   { 3               Void  Message ();  4   }  5           Public   Abstract   Class  Myclass: imsg  6   {  7               Public   Virtual   Void  Message () 8   {  9 Console. writeline ( "  Myclass  "  );  10   }  11   }  12   13           Public   Class  Myderivedclass: myclass, imsg 14   {  15   }  16   17           Public   Class  Effectivecsharp  18   {  19               Public   Static   Void Main ( String  [] ARGs) 20   {  21 Myderivedclass d = New  Myderivedclass ();  22   D. Message ();  23 Imsg M = d As  Imsg;  24   M. Message ();  25 Myclass c = (Myclass) m; 26   C. Message ();  27   Console. Read ();  28   }  29 }

Running output:

 

Section

There are more options for implementing interfaces than creating and overwriting virtual functions. We can create sealed implementation, virtual implementation, or abstract implementation for the class hierarchy. We can also create a sealed implementation and provide virtual method calls in methods that implement interfaces. We can also determine how and when to modify the default behavior of interface members implemented in the base class. The interface is not a virtual method, but a separate contract.

Related Article

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.