Sealed keyword Learning

Source: Internet
Author: User
The Chinese meaning of sealed is sealed. The old name "sealed" means that the classes or Methods Modified by sealed cannot be inherited or overwritten.

The role of the sealed Keyword:

Using sealed in class declaration can prevent other classes from inheriting this class; using sealed modifier in method declaration can prevent extended classes from rewriting this method.

The sealed modifier is mainly used to prevent unintentional derivation, but it can also promote some runtime optimization. Specifically, because the sealed class will never have any derived class, the call to the virtual function Member of the sealed class instance can be converted to a non-virtual call for processing.

Sealing class:

The sealed modifier is used in the Declaration to prevent the class from being inherited by other classes. If you try to use a sealed class as the base class of other classes, C # will prompt an error. Of course,The sealed class cannot be an abstract class at the same time, because the abstract always wants to be inherited..

In what scenarios are the seals used? In fact, there cannot be a derived class in the seal class. If there is a virtual member function in the sealed instance, the member function can be converted to non-virtual, and the function modifier virtual will no longer take effect.

Let's take a look at the following example:

 

1 Abstract Class Abstractclass
2 {
3 Public   Abstract   Void Method ();
4 }

5 Sealed   Class Sealedclass: abstractclass
6 {
7 Public   Override   Void Method ()
8 {//...}
9 }

 

If we try to write the followingCode

Class Otherclass: sealedclass
{
}

C # will indicate this error and tell you that sealedclass is a sealedclass and cannot be used to derive any classes from sealedclass.

Sealing Method:

C # also proposedSealedmethod)To avoid overloading the method in the class of the method. You can use the sealed modifier for the method. In this case, we call this method a sealed method.

Not every member method of the class can be used as a sealing method. to use it as a sealing method, the virtual method of the base class must be overloaded to provide specific implementation methods. So,In method declaration, the sealed modifier is always used together with the override Modifier. See the following example code:

 

1 Using System;

2 Class A
3 {
4 Public   Virtual   Void F ()
5 {
Console. writeline ( " A.f " );
}
6
Public   Virtual   Void G ()
7 {
Console. writeline ( " A. G " );
}
8 }

9 Class B:
10 {
11 Sealed   Override   Public   Void F ()
12 {
Console. writeline ( " B. f " );
}
13
Override   Public   Void G ()
14 {
Console. writeline ( " B .g " );}
15 }
16
Class C: B
17 {
18 Override   Public   Void G ()
19 {
Console. writeline ( " C. g " );
}
20 }

Class B loads both Virtual Methods in base class A. The F method uses the sealed modifier to become a sealing method. The G method is not a sealing method. Therefore, in the C Class of B, you can overload method G, but not method F.

Refer:

    • C # class Inheritance Mechanism
    • Using sealed class in. net

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.