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        {public            virtual void fun1 ();        }

Error 2 "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        {public            abstract void Fun2 () {}        }

Error 1 "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    {public       virtual void fun () {}//must have implementation    }    class Derivetest1:basetest1    {        // public override void Fun () {}    }

The compilation will not have an error, if you override the virtual decoration method, you must add override (this tells the compiler you want to override the virtual method), and must be implemented, or compile error;

    Abstract class BaseTest2    {public        abstract void Fun ();    }    Class Derivetest2:basetest2    {        //public override void Fun (); Error 1: No implementation        //public  void Fun () {}  Error 2: Override did not add override        //override void Fun () {} Error 3: Virtual member or abstract member cannot be private (as long as the virtual member or abstract member is declared in the parent class, even if the inheritance is added with this restriction)        public override void Fun () {}//if overridden 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 class Test        {            //public virtual void Prinf (); Error: Virtual method must have implementation public            virtual void Prinf ()// The virtual method of the abstract class can be overridden, and the abstract method must be overridden.            {                Console.WriteLine ("Abstract Printf ...");}        }        public class Class1:test        {            /* public            override void Prinf ()//The virtual method in the derived class that does not override the abstract class can still be run, However, when invoking the printf method of a derived class object, the parent class is called.            {                Console.WriteLine ("Class one Override Printf ...");            }             */        }

The difference between virtual and abstract in C #

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.