The difference between virtual and abstract in ASP.

Source: Internet
Author: User

First, virtual method (virtual method)

The virtual keyword is used to decorate a method in a base class. There are two things you can do with virtual:
Scenario 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. In a call to a derived class instance, the virtual method uses the method defined by the base class.
Scenario 2: The virtual method is defined in the base class and then overridden in a derived class by using override. In a call to a derived class instance, the virtual method uses the derived overridden method.

When a method is declared as virtual, it is a dummy method until you use ClassName variable = new ClassName (); Before declaring an instance of a class, it does not exist in the real memory space. This keyword is very common in class inheritance and is used to provide polymorphism support for class methods.

Ii. Abstract Method (Abstraction method)
The abstract keyword can only be used to decorate a method in an abstraction class, and there is no specific implementation. An implementation of an abstract method must be implemented in a derived class by using the override keyword.

Abstract method declarations are used as methods that must be overridden by derived classes, which are used to be inherited, and can be seen as virtual methods that do not implement a body, and if the class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods, and the abstract class cannot have entities.

Three, polymorphic

The implementation of polymorphism in C # is divided into two types, one is compile-time polymorphism and one is run-time polymorphic

|-compile-time polymorphism through the overloads of multiple methods in a class, the system at compile time, according to the parameters passed to determine which overloaded method of the specific call;

|-Runtime polymorphism by virtual function (virtual function), abstract method implementation of polymorphism, derived classes to override virtual functions or abstract methods, so as to achieve runtime polymorphism.

Iv. usage of abstract

C # codeCopy
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace ConsoleApplication3
{Public    abstract class book    {        //abstract method, without body, abstract method where the class must be an abstract class, the derived class must implement the method public        abstract void Introduce ();    }    public class Javabook:book    {        //implement abstract methods, must be implemented, note! The override key must be added public        override void introduce ()        {            Console.WriteLine ("I ' m Java");        }    }    public class Test    {public        test ()        {Test t = new test ();     }}}

V. Usage of virtual and the use of override

C # codeCopy
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace ConsoleApplication2
{Public abstract class book{public virtual void introduce (){Console.WriteLine ("I m book"); } public virtual void Sayhi (){Console.WriteLine ("Hi, I ' m book"); }} public class Javabook:book {public override void introduce () {Console.WriteLine ("I ' M Java "); }//Note that this method does not override the parent class's method public void Sayhi () {Console.WriteLine ("Hi, I ' m Java");}} public class test {public Test () {Javabook Javabook = new Javabook (); Book book = new Javabook (); Javabook.introduce (); Will call Javabook in introduce () book. Introduce (); Will call Javabook in introduce () Javabook.sayhi (); Will call Javabook in Sayhi () book. Sayhi (); Will call book in Sayhi ()} public static void Main () {Test t = new test ();       }}}

Vi. the difference between virtual and abstract

(1), virtual modification of the method must be implemented (even if only to add a pair of curly braces), and the method of the abstract modification must not be implemented. If the method for the virtual modification is not implemented:

(3), if the class member is abstract decorated, then the class must be added abstract, because only abstract classes can have an abstract method.

(4), cannot create an instance of the abstract class, can only be 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), 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).

The difference between virtual and abstract in ASP.

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.