Rewrite, hide, abstract, and Polymorphism

Source: Internet
Author: User
  • Virtual is used in the base class. specifying a virtual method (attribute) indicates that this method (attribute) can be rewritten.
  • Override is used in a derived class to overwrite the basic class virtual method (attribute.

The above base classes and derived classes are opposite. B is the base class of C or the derived class of A. B can override the virtual method in a or specify the virtual method for C to rewrite.

  • You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override. Why can override be rewritten? Because the override in the base class is actually a rewriting of the base class, because inheritance can be passed, you can also override the override method in the base class.
  • Override declares that the accessibility of the virtual method cannot be changed. The override and virtual methods must have the same access level modifier.
  • You cannot use modifiers new, static, virtual, or abstract to modify the override method.
  • The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the overwritten attribute must be virtual, abstract, or override.
Public class baseclass {Public Virtual string getstring () {return "is a virtual method. ";}} Public class derivedclass: baseclass {public override string getstring () {return" This Is A Rewriting Method. ";}}
 
 
Example
Public class baseclass {Public String getstring () {return "is a method of the base class. ";}} Public class derivedclass: baseclass {public new string getstring () {return" this is a method that hides the base class getstring. ";}}

We can see that the hidden keyword is new.

 

 

 

When we analyze the problem, the more we analyze it, the more abstract the structure is. For example, we analyze the animal hunting behavior: these animals are group cooperation, those animals are waiting patiently, those animals are a fatal blow ...... They all have a method called "Hunting", but we cannot give them a unified process. At this time, we can define an abstract method in their base class. This method does nothing but occupies a name.

public abstract class HuntingAnimal{public abstract void Hunt();}public class Tiger : HuntingAnimal{public override void Hunt(){//...}}

Huntinganimal defines an abstract method Hunt () with abstract. Since abstract methods do not do anything, braces are not required. Simply end the process with quotation marks. In a derived class, override is used to implement this abstract method.

  • Abstract classes cannot be instantiated, so new cannot be used to generate instances.
  • If the method is abstract, the class must be abstract.
  • The derived class must implement all abstract methods in the base class. If it cannot, it should also be an abstract class.
  • Abstract classes cannot be sealed. (Seal: if we don't want a class to be inherited, we can use the sealed keyword to ensure that it will not be inherited .)

Note that although both the virtual and abstract methods use the override keyword to overwrite, the virtual and abstract methods are completely different.

 

 

Generally, polymorphism refers to "Runtime polymorphism", that is, the system determines the ability to call the corresponding methods of the class of the object according to different objects when the program is running.

Example

Namespace cftea {public class baseclass {Public Virtual string getstringvirtual () {return "base class virtual method. ";} Public String getstring () {return" base class method. ";}} Public class derivedclass: baseclass {public override string getstringvirtual () {return" the override method of the derived class. ";} Public new string getstring () {return" hidden method of the derived class. ";}} Public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_load (Object sender, eventargs e) {derivedclass = new derivedclass (); baseclass = derivedclass; MessageBox. show ("derivedclass. the getstringvirtual () result is: "+ derivedclass. getstringvirtual () + "\ r \ n" + "baseclass. the getstringvirtual () result is: "+ baseclass. getstringvirtual () + "\ r \ n" + "derivedclass. the getstring () result is: "+ derivedclass. getstring () + "\ r \ n" + "baseclass. the getstring () result is: "+ baseclass. getstring ());}}}

The running result is:

We can see that the derivedclass and baseclass types are different, but they all point to new derivedclass ():

  • They use derived classes when calling getstringvirtual. (Polymorphism)
  • They use methods of their respective types when calling getstring.

Here we can see the difference between rewriting and hiding.

 

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.