C # differences between abstract and Virtual Methods

Source: Internet
Author: User
Virtual and abstract methods can be rewritten by the derived classes. What is the difference between them?

1. The virtual method must have an implementation part and provide the option to overwrite the method for the derived class;
The abstract method does not provide the implementation part. The abstract method is a method that forces the override of the derived class. Otherwise, the derived class cannot be instantiated.
For example:
// Abstract Method
Public abstract class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}
// Virtual Method
Public class animal
{
Public Virtual void sleep (){}
Public Virtual void eat (){}
}

2. abstract methods can only be declared in abstract classes. abstract methods must be rewritten in Derived classes;
Virtual methods are not and do not need to be rewritten. In fact, if a class contains abstract methods, the class is also abstract and must be declared as abstract. For example:
Public class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}
The compiler reports the following error:
Main. CS (10): 'vstest. Animal. Sleep () 'is abstract but it is contained in nonabstract class 'vstest. Animal'
Main. CS (11): 'vstest. Animal. Eat () 'is abstract but it is contained in nonabstract class 'vstest. Animal'
3. the abstract method must be rewritten in the derived class. This is similar to the interface, and the virtual method is not required. Abstract METHODS cannot declare method entities. Virtual methods can contain classes of abstract methods and cannot be instantiated. Classes containing virtual methods can be instantiated! For example:
Public abstract class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}

Public class Cat: Animal
{
Public override void sleep ()
{
Console. writeline ("cat is sleeping ");
}
// We need implement animal. Eat () here
}

The compiler reports the following error: Main. CS (14): 'vstest. cat 'does not implement inherited abstract member' vstest. animal. eat () ', because we have not implemented all abstract methods in the abstract class.

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.