Jump to content
  • Advertisement
Sign in to follow this  
kitman20022002

Why people say a Sealed Classes cannot be a base class in C#

This topic is 871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why people say a Sealed Classes cannot be a base class. because for example 
 
    namespace sealed_class
    {
        class Program
        {
           //////////////////////////////////
           //Now, sealed = BaseClass ,so that's why I don't understnad** 
           //////////////////////////////////
            public sealed class BaseClass
            {
                public void Display()
            {
                Console.WriteLine("This is a sealed class which can;t be further inherited");
            }
        }
     
            public class Derived : BaseClass
            {
                // this Derived class can;t inherit BaseClass because it is sealed
            }
        }
    }
could someone explain to me why I'm wrong ,thanks.
Edited by kitman20022002

Share this post


Link to post
Share on other sites
Advertisement
The reason is right there in your link:

True, because sealed class can not be inherited.seales keyword is used for making non-inheritable class.


Making a class unable to be further inherited is the entire point of the "sealed" keyword.

Share this post


Link to post
Share on other sites

Not really sure what you are misunderstanding... your code doesn't agree with your question.

 

A base class is a class which other classes inherit from.

The "sealed" keywords' purpose is to disallow further inheritance.

This means a "sealed" class can not be a base class. 

(personally, I'd rather just say "a sealed class can not be inherited from", instead)

 

So you will get a compilation error on the line "  public class Derived : BaseClass"

 

More info on "sealed" here: https://msdn.microsoft.com/en-us/library/88c54tsw.aspx

Share this post


Link to post
Share on other sites

As others mentioned he "sealed" keyword is meant to *prevent* inheritance.

 

what you might wanna do is the "abstract" keyword, that do not let the base class be instantiated.

 

so, let's say you have this code:

public abstract class BaseClass  { } 
public class ChildClass : BaseClass {}

the we have :

public void SomeMethod() { 
    new BaseClass(); // compile error
    new ChildClass(); // totally fine.
}

Also, you might be tring to hide the class from external sources.

 

you could mess around with *protected*, *internal* and *private*.

But with classes in namespace level, you will have some restrictions (and I advise to research a little bit about those first =D )

And, by your code, classes inside another class is one thing I don't like very much

Edited by Kakyo

Share this post


Link to post
Share on other sites

 

           //////////////////////////////////
           //Now, sealed = BaseClass ,so that's why I don't understnad** 
           //////////////////////////////////
 

 

In your example BaseClass is just a name, it means nothing to the compiler apart from its name. Just calling a class BaseClass does not make sealed = BaseClass true in the same way that "public class SealedClass {}" does not make SealedClass sealed, you can inherit from it

 

 

A base class is a semantic thing meaning it is used for other classes to inherit from, they are normally abstract but that is also not a requirement. Some people will have Base in the class name but again that is not a requirement. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!