C# training video: - How can we stop the class from further inheriting?

In this c# article we will try to understand how we can further stop the class from inheriting. With this article we have also attached a video in which we have demonstrated how the c# sealed keyword helps you to avoid inheritance.

You can also watch C# training video below.

We can stop the class from further inheriting by using the "Sealed" keyword. For instance below is a simple sample code where we have a class called as "Human" which is further inherited to create a "Male" or "Female" class.

Now the below code is great but we do not anyone to further inherit from "Male" or "Female" class. In simple "Male" and "Female" are the last legs in inheritance. This can be done by using the "Sealed" keyword.

public class Human
public sealed class Male : Human
public sealed class Female : Human

If anyone tries to inherit the sealed classes he will end with the below error "cannot derive from sealed type". You can also watch the below video to see how these things work practically.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: