In this chapter you will learn
- What is
Sealed
class? - Programming Examples and Codes
What is Sealed Class?
A sealed
class cannot be a base class and it prevents derivation. Child class cannot inherit sealed class. A sealed class can be defined by putting sealed
keyword before the class name.
public sealed class D { // Class members here. }
Programming Example and Codes
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sealed_Class { class Program { static void Main(string[] args) { SealedClass s = new SealedClass(); s.message(); Console.ReadKey(); } } public sealed class SealedClass { public void message() { Console.WriteLine("Hey, I am Sealed Class"); } } // public class child : SealedClass // { // message(); // } }
If you uncomment child class, it will raise compile time error.
OutputHey, I am Sealed Class
_
Summary
In this chapter you learned how to use Sealed
class to hide base class from child. Sealed
keyword locks the class so it is hidden from child class. In the next chapter you will learn Inheritance by programming examples.