This site features a collection of common technical interview questions gathered by a group of programmers who have been through, and given, lots of technical interviews. There is an emphasis on C++ and game programming technical interviews, but most of the questions are relevant to any technical interview.
Block Inheritance

In C#, can you prevent a class from being inherited by another class? If so, how do you do it? And why would you want to do it?


10 Comments so far
Leave a comment

This can be done by making constructors of the class private and making use of Named constrcutors for the class

In C# the proper way to prevent a class from being inherited is to use the “sealed” modifier.

Sealed classes

try the ‘sealed’ keyword on the class definition.

sealed class CantInheritMe {}

The above comment is incorrect. Making a class constructor private will not prevent a class from being inherited by another class, it would simply prevent it from being instantiated. The desired functionality would be accomplished by using the “sealed” keyword in the class declaration. (i.e. public sealed class MyClass).

The sealed modifier is primarily used to prevent unintended derivation, such as when a class contains a concrete implementation of an abstract base. In this case the developer of the sealed class would want other implementations to inherit from the abstract base class and instead of inheriting from the sealed concrete implementation. It also enables certain run-time optimizations. In particular, because a sealed class is known to never have any derived classes, it is possible to transform virtual function member invocations on sealed class instances into non-virtual invocations.

Yes, use the sealed modifier as follows:

sealed class NotInheritable { … }

You would use this to prevent unintential inheritance, or for run-time optimization.

This can be done by declaring the class as a sealed class.

This test is not related to what i do at work, it is designed for people that work with object orianted programming, so I dont see value in me doing this test.

sealed modifier is used to keep the class inherited.

sorry i mean sealed modifier is used to prevent a class from being inherited by another class. some class are not be inherited in future. so ti inheriting this class by another class we used sealed.

I once froze when asked, in an interview for a gruatade traineeship at Grant Thornton, to name a time I had been wrong about something. A bit of an unfair question I thought. The interviewer clearly didn’t account for the possibility that someone might never have been wrong about anything in their entire life. I didn’t get the job and I’m certain it was down to flubbing that one question.



Leave a comment
If you are including code in your comment, place it within a <div class='code'></div> tag for better formatting.


Do you have a technical interview question you would like to submit? Some tips you would like to pass on? Just want to say hi? Feel free to contact us