Differences between new, virtual, abstract, sealed, and override

Source: Internet
Author: User
New

The New Keyword can explicitly hide members inherited from the base class. Hiding inherited members means that the derived version of the member replaces the base class version. That is to say, the method used in the derived class is to call the method with the new keyword.New definitionInstead of the base class method.
Hidden members are allowed without the new modifier, but warnings are generated. Explicitly hiding a member with new removes this warning and records the fact that the member is replaced with a derived version.

Virtual

The virtual keyword allows you to override these objects in a derived class. By default, the method is non-virtual and cannot be rewritten, virtual keywords cannot be used with static, abstract, private, or override.
The virtual keyword is closely related to override. To implement the virtual method, you must use the override or New Keyword (new and override generate different mechanisms ).

Abstract

Use the abstract modifier in the class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or included in an abstract class must be implemented through classes derived from the abstract class. Abstract classes cannot be instantiated.
Abstract method declaration does not provide actual implementation, so there is no method body; method declaration ends with a semicolon and there is no braces ({}) after the signature ({}).

Sealed

The sealing method will override the methods in the base class, but it cannot be further rewritten in any derived class. The sealed class cannot be inherited.
When applied to methods or attributes, the sealed modifier must always be used with override.

Override

The override keyword mainly provides a new implementation of the derived class to the base class method. The overwritten base class method must have the same signature as the override method.

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.