The difference between abstract methods and virtual methods in C #

Source: Internet
Author: User
Tags modifiers

An abstract method: defined only in abstract classes, method modifiers cannot use Private,virtual,static.

The abstract method is as follows:

Public abstract class people//declare an abstract category

{

public abstract Void Study (); Abstract methods can only be defined in abstract classes.

}

public class Student:people//Inherit abstract class

{

public override void Study ()//override abstract method of abstract class

{

Console.WriteLine ("Study hard, day up!");

}

}

public class Program

{

static void Main (string[] args)

{

Student t= new Student ();//instantiation of derived classes

People p= t; Instantiating an abstract class using a derived class object

The above two sentences are equivalent to people p = new Student ();//Instantiate an abstract class with a derived class object;

P.study (); Use abstract class objects to invoke abstract methods in an abstract class study

}

}

Summary: (1) The image method can only be declared in an abstract class, using the keyword abstract

(2) Abstract methods in abstract classes must be overridden by the quilt class.

"Abstract methods have no method body, subclasses must override the method body!", so the abstract method can be seen as a virtual method without a method body "

Second, virtual method: Using the virtual modification method:

A virtual method can have a method body. Specific examples are as follows:

public class BaseClass//Create a base class

{

Public virtual string GetName ()//using the virtual keyword to create a virtual method in a parent class

{

Return "Parent virtual method Body":

}

}

public class Subclass:baseclass//subclass Inherits Parent class

{

public override string GetName (); Subclasses overriding parent virtual methods

{

Return "overriding the virtual method of the parent class!";

}

}

The example above: a virtual method in a parent class is overridden by a derived class.

Note: The virtual modifier cannot be used in conjunction with the private, static, abstract, and override modifiers.

The Ps:override modifier cannot be used in conjunction with the new, static, virtual modifiers, and the overriding method can only be used to override virtual methods in the base class.

Limitations of Virtual functions:

* Virtual functions only apply to class objects with inheritance, so only the member functions of the class can be described as virtual functions;

* Static member functions, inline functions, constructors cannot be virtual functions;

* Destructors can be virtual functions.

Three, the difference between the two:

Summary: The abstract method is only the method name, there is no method body (that is, no method specifically implemented), the subclass must override the parent class abstract method;

Virtual functions are methods that have a method body, but subclasses can be overridden or not overwritten.

(1) There are methods for virtual methods, and there is no method body for abstract methods. An abstract method is a method that enforces the override of a derived class, otherwise the derived class cannot be instantiated;

(2) Abstract methods can only be declared in the abstract class, the virtual method is not;

(3) A derived class must override an abstract method in an abstract class, but a virtual method is unnecessary.

The difference between abstract methods and virtual methods 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.