How to use virtual, override, sealed, abstract

Source: Internet
Author: User

Virtual {method body}

Abstract {method bodies are not allowed}

The C # method introduces virtual, override, sealed, and abstract modifiers to provide different inheritance requirements. Class virtual methods can introduce virtual, override, sealed, and abstract modifiers in Class C # inheritance to provide different inheritance requirements. The virtual method of a class can change its implementation method in the inherited class of the class. Of course, this change is limited to the change of the method body, rather than the change of the method header (method declaration. The virtual method changed by the quilt class must be indicated by override in the method header. When a virtual method is called, the instance of this class, that is, the run-time type of the object, determines which method is called. See the following example:
Using System;

Class Parent

{

Public void F ()

{

Console. WriteLine ("Parent. F ");

}

 

Public virtual void G ()

{

Console. WriteLine ("Parent. G ");

}

}

Class Child: Parent

{

New public void F ()

{

Console. WriteLine ("Child. F ");

}

 

Public override void G ()

{

Console. WriteLine ("Child. G ");

}

}

Class Test

{

Static void Main ()

{

Child B = new Child ();

Parent a = B;

A. F ();

B. F ();

A. G ();

B. G ();

}

}

After the program is compiled and executed, the output is:

Parent. F

Child. F

Child. G

Child. G

We can see that the F () method declaration in the Child class adopts the rewrite (new) method to shield the non-virtual method F () declaration in the parent class, while G () methods Use override to provide method polymorphism. Note that the rewrite (new) method is different from the override method. In essence, the rewrite method is bound at compile time, and the override method is bound at runtime. It is worth noting that virtual methods cannot be static methods -- that is to say, static and virtual methods cannot be modified simultaneously, which is determined by its runtime type discrimination mechanism. Override must be used together with virtual, so it cannot be used together with static.

What should the inheritance system do if it does not want to overwrite a virtual method in a class? The answer is sealed override (sealed override). You can use sealed and override to simultaneously modify a virtual method: sealed override public void F (). Note that sealed and override must be used at the same time. sealed must also cover a virtual method or a virtual method that is overwritten (instead of sealed. Sealing a non-virtual method is meaningless and incorrect.

Abstract methods are logically similar to virtual methods, but cannot be called as virtual methods. They are declarations rather than implementations of an interface. Abstract methods cannot be implemented or implemented. Abstract methods cannot be static. Classes containing abstract methods must be abstract classes and abstract class modifiers must be added. Abstract classes do not have to contain abstract methods. The subclass of an abstract class that inherits an abstract method must be overwritten and implemented (override is used directly). This method can be combined with abstract override to continue abstraction, or no overwrite or implementation is provided, the behavior of the latter two is the same. See the following example:

Using System;

Abstract class Parent

{

Public abstract void F ();

Public abstract void G ();

}

Abstract class Child: Parent

{

Public abstract override void F ();

}

Abstract class Grandson: Child

{

Public override void F ()

{

Console. WriteLine ("Grandson. F ");

}

Public override void G ()

{

Console. WriteLine ("Grandson. G ");

}

}

Abstract METHODS can abstract an inherited virtual method. By grasping the basic mechanism of binding during runtime and compilation, you can see through the overload, virtual, override, sealed, abstract and other methods. Class to change its implementation method, of course, this change is limited to the change of the method body, rather than the change of the method header (method declaration. The virtual method changed by the quilt class must be indicated by override in the method header. When a virtual method is called, the instance of this class, that is, the run-time type of the object, determines which method is called. See the following example: using System; class Parent {public void F () {Console. writeLine ("Parent. F ");} public virtual void G () {Console. writeLine ("Parent. G ") ;}} class Child: Parent {new public void F () {Console. writeLine ("Child. F ");} public override void G () {Console. writeLine ("Child. G ") ;}} class Test {static void Main () {Child B = new Child (); Parent a = B;. F (); B. F ();. G (); B. G () ;}} program compiled and executed, output: Parent. F Child. F Child. G Child. g, we can see that the F () method declaration in the Child class adopts the rewrite (new) method to shield the non-virtual method F () declaration in the parent class, while G () methods Use override to provide method polymorphism. Note that the rewrite (new) method is different from the override method. In essence, the rewrite method is bound at compile time, and the override method is bound at runtime. It is worth noting that virtual methods cannot be static methods -- that is to say, static and virtual methods cannot be modified simultaneously, which is determined by its runtime type discrimination mechanism. Override must be used together with virtual, so it cannot be used together with static. What should I do if I do not want to overwrite a virtual method in a class inheritance system? The answer is sealed override (sealed override). You can use sealed and override to simultaneously modify a virtual method: sealed override public void F (). Note that sealed and override must be used at the same time. sealed must also cover a virtual method or a virtual method that is overwritten (instead of sealed. Sealing a non-virtual method is meaningless and incorrect. Abstract methods are logically similar to virtual methods, but cannot be called as virtual methods. They are declarations rather than implementations of an interface. Abstract methods cannot be implemented or implemented. Abstract methods cannot be static. Classes containing abstract methods must be abstract classes and abstract class modifiers must be added. Abstract classes do not have to contain abstract methods. The subclass of an abstract class that inherits an abstract method must be overwritten and implemented (override is used directly). This method can be combined with abstract override to continue abstraction, or no overwrite or implementation is provided, the behavior of the latter two is the same. Take the following example: using System; abstract class Parent {public abstract void F (); public abstract void G ();} abstract class Child: parent {public abstract override void F ();} abstract class Grandson: Child {public override void F () {Console. writeLine ("Grandson. F ");} public override void G () {Console. writeLine ("Grandson. G ") ;}} abstract methods can abstract an inherited virtual method. By grasping the basic mechanism of binding during runtime and compilation, you can see through the overload, virtual, override, sealed, abstract and other methods.

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.