Sealed Class
This article is used to make you understand the real fundamentals of sealed class, why we need sealed class. This article also help you to answer questions which comes to our mind while understanding sealed class.Lets get started..........
What is Sealed Class
Sealed class used to restrict inheritance in Object Oriented Programming. Sealed keyword used to make the class sealed.See the below Image . It shows something is sealed for further extension.
Sealed Class in C# |
Skeleton
sealed class Parent
{
}
Lets understand without sealed class how the class behave and after the implementation of Sealed class how behavior of the class changed.
Without sealed keyword
public class Parent
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
public class Child : Parent
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
public class Child : Parent
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
Above code will work fine , but look at the below code where we used sealed keyword along with the inheritance.
With sealed keyword
public sealed class Parent
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
public class Child : Parent
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
public class Child : Parent
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
The above code will through compiler error as shown below.
Sealed Keyword |
We understood why we need sealed class .
Now One more question comes to our mind .........
can we create an object of the sealed class?
You can find this question in interviews right...
Lets see this in action.
class SealedClass
{
public static void Main()
{
Parent p = new Parent();
p.method1();
}
}
public sealed class Parent
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
{
public static void Main()
{
Parent p = new Parent();
p.method1();
}
}
public sealed class Parent
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
In the above code we can see that we can able to create object of the Parent class which is a sealed class.
Can we Implement other class to a sealed class?
The above question is very tricky...
This means Let say we have 2 classes A and B.
Lets assume A- Sealed class and B - Not a sealed class.
Can we write A:B?
Lets see in action.
public class parent //Non sealed class
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
public sealed class child:parent //Child is sealed class
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
{
public void Method2()
{
Console.WriteLine("Child Method...");
}
}
public sealed class child:parent //Child is sealed class
{
public void method1()
{
Console.WriteLine("Parent Method...");
}
}
In the above code Child class declared as sealed and also child class inherited from parent class . This case code executed successfully.
Hence this is proved that Other class can implement sealed class.
So answer to the above question Can we write A:B? -Yes
Things to Remember
- Sealed class restrict a class to be inherited.
- We can create object of the sealed class
- We can inherit other class to the sealed class.
No comments:
Post a Comment