The difference between virtual and abstract in C #

Source: Internet
Author: User

Both virtual and abstract are used to decorate the parent class by overriding the definition of the parent class to redefine the subclass.

They have one thing in common: if you are using a method, you must add public to the front, or you will get a compilation error: a virtual or abstract method cannot be private. After all, adding virtual or abstract is to redefine the subclass, and private members cannot be accessed by the quilt class.

But they are quite different. (Virtual is "fictitious", Abstract is "abstracted").

(1) The method of virtual modification must be implemented (even if only a pair of curly braces are added), and the method of the abstract modification cannot be implemented. if the method for the virtual modification is not implemented:

 Public class Test1        {            publicvirtualvoid  fun1 ();        }

Error: "TEST1.FUN1 ()" must declare the principal because it is not marked as abstract, extern, or partial

For the method of the abstract modification, if it is implemented:

 Public Abstract class Test2        {            publicabstractvoid  fun2 () {}        }

Error: "Test2.fun2 ()" Cannot declare the principal because it is marked abstract

(2) virtual can be overridden by the quilt class, and abstract must be overridden by the quilt class.

class BaseTest1    {       publicvirtualvoid fun () {}// must have implementation     }     class Derivetest1:basetest1    {        //publicoverride void Fun () {}    }

There is no error in compiling, and if you override the virtual decoration method, you must add override (which tells the compiler you want to override the virtual method) and must be implemented, otherwise the compilation error occurs:

Abstract classBaseTest2 { Public Abstract voidFun (); }    classDerivetest2:basetest2 {//Public override void Fun (); Error 1: No implementation//Public Void Fun () {} error 2: Override was not added when overridden//override void Fun () {} Error 3: Virtual members or abstract members cannot be private (as long as the virtual member or abstract member is declared in the parent class, even if the inheritance is added to this restriction)         Public Override voidFun () {}//if overriding method; Error: "A.derivetest2" does not implement inherited abstract member "A.basetest2.fun ()"    }

(3) If a class member is abstract, then the class must be added abstract, because only abstract classes can have abstract methods.

(4) cannot create an instance of the abstract class, only inherited cannot be instantiated , such as: BaseTest2 base2 = new BaseTest2 (); A compilation error will occur: An abstract class or interface cannot create an instance.

(5) In C # If you want to override a method in a subclass, you must add virtual to the parent class method before the subclass method, adding override before the child class, thus avoiding the programmer accidentally overriding the parent class method in the subclass.

(6) The abstract method must be overridden, and the virtual method must be implemented (even if it is a method defined in the abstract class).

Abstract  Public classTest {//Public virtual void Prinf (); Error: The virtual method must have an implementation             Public Virtual voidPrinf ()//the virtual method of the abstract class can be overridden, and the abstract method must be overridden. {Console.WriteLine ("Abstract Printf ..."); }        }         Public classClass1:test {/*Public override void Prinf ()//The virtual method in the derived class that does not override the abstract class can still be run, but when the printf method of the derived class object is called, the parent class is called.            {Console.WriteLine ("Class one Override Printf ..."); }             */        }

Transferred from: http://www.cnblogs.com/wang7/archive/2012/04/17/2453624.html

The difference between virtual and abstract in C #

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.