Differences between virtual and abstract methods

Source: Internet
Author: User

Virtual method: it is a method modified with the virtual keyword and implemented in one or more Derived classes. If a method is virtual, it cannot be modified by static, abstract, or override.

Abstract method: it is a method modified by the abstract keyword. abstract methods can be considered as virtual methods without entity implementation and must be overwritten in the derived class. If a class includes abstract methods, this class is an abstract class.

(1) Virtual Methods

1. Virtual methods must contain implementations. Abstract METHODS cannot contain implementations;

2. Virtual can be overwritten by the quilt class, while abstract must be overwritten by the quilt class;

Class base
{
Public Virtual void myfunc (string SDD)
{
String STR = "DD" + SDD;
}
}

Class derived: Base
{
Public override void myfunc (string SS)
{
String STR = "DDS" + SS;
}
}

(2) Abstract METHODS

1. If the class members are modified in abstract, abstract must be added before the class, because only abstract classes can have abstract methods.

Abstract class base
{
Public abstract void myfunc (string SDD)
{
String STR = "DD" + SDD; // error. The subject cannot be declared because it is marked as abstract
}
}

2. Abstract must be overwritten by the quilt class

Abstract class base

{

Public abstract void myfunc ();

}

Class derived: Base
{
Public override void myfunc ()
{

}
}

 

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.