Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a class protected for inheritance?

Tags:

c#

.net

oop

I would like to do the same thing I do in Java with the final keyword. I tried to use the const keyword, but it doesn't work. How can I prevent other classes from inheriting from my class?

like image 752
Mister Dev Avatar asked Dec 12 '08 20:12

Mister Dev


People also ask

How can we prevent a class to be inherited?

You can prevent a class from being subclassed by using the final keyword in the class's declaration. Similarly, you can prevent a method from being overridden by subclasses by declaring it as a final method. An abstract class can only be subclassed; it cannot be instantiated.

Can protected members of a class be inherited?

We can access protected members of a class in its subclass if both are present in the same package.

Can classes be inherited?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

Are protected members inherited C++?

With protected , all public members of the base class are inherited as protected in the derived class. Conversely, if the most restricting access level is specified ( private ), all the base class members are inherited as private .


1 Answers

The keyword you are searching is "sealed".

MSDN

like image 154
Patrick Desjardins Avatar answered Oct 20 '22 21:10

Patrick Desjardins