Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a class not inherited

I am trying to create a c# class, but I dont want it to be inherited. How can I accomplish that?

like image 819
Troy Avatar asked Aug 23 '10 04:08

Troy


People also ask

How do you make a class not inherited?

In C# you can use the sealed keyword in order to prevent a class from being inherited. Also in VB.NET you can use the NotInheritable keyword to prevent accidental inheritance of the class.

How do you make a class non inheritable in C++?

In C++ 11, we can make the base class non-inheritable by using the final specifier. For eg, the following code gives a compile error as the base class is declared as final.

Is it possible to restrict inheriting a class in Java?

There are 2 ways to stop or prevent inheritance in Java programming. By using final keyword with a class or by using a private constructor in a class.


1 Answers

sealed is the word you're looking for, and a link for reference

public sealed class MyClass {  } 

And then just create your class as normal, however you won't be able to inherit from it.

You can however still inherit from a different class like so

public sealed class MyClass : MyBaseClass {  } 
like image 135
PostMan Avatar answered Sep 18 '22 03:09

PostMan