Please go through the Sealed method details first before.
Private method is used to restrict the access of the method outside the class, whereas Sealed method is used to restrict the further override of the method.
Private method can not be accessed from child class, but Sealed method can be accessed from child class as well, but Sealed method can avoid further redefining and overriding of the method.
E.G.
namespace SealedMethodDemo
{
class ParentClass
{
private void PrivateMethod()
{
}
protected void ProtectedMethod()
{
}
public void PublicMethod()
{
}
public virtual void SealedMethod()
{
}
}
class ChildClass : ParentClass
{
public void Test()
{
PublicMethod();
ProtectedMethod();
SealedMethod();// Can access Sealed method but can not access PrivateMethod
}
public sealed override void SealedMethod()
{
base.SealedMethod();
}
}
class Program: ChildClass
{
static void Main(string[] args)
{
ChildClass childObject = new ChildClass();
childObject.SealedMethod();
}
//public override void SealedMethod() //cannot override inherited member because it is sealed
//{
//}
}
}
Private method is used to restrict the access of the method outside the class, whereas Sealed method is used to restrict the further override of the method.
Private method can not be accessed from child class, but Sealed method can be accessed from child class as well, but Sealed method can avoid further redefining and overriding of the method.
E.G.
namespace SealedMethodDemo
{
class ParentClass
{
private void PrivateMethod()
{
}
protected void ProtectedMethod()
{
}
public void PublicMethod()
{
}
public virtual void SealedMethod()
{
}
}
class ChildClass : ParentClass
{
public void Test()
{
PublicMethod();
ProtectedMethod();
SealedMethod();// Can access Sealed method but can not access PrivateMethod
}
public sealed override void SealedMethod()
{
base.SealedMethod();
}
}
class Program: ChildClass
{
static void Main(string[] args)
{
ChildClass childObject = new ChildClass();
childObject.SealedMethod();
}
//public override void SealedMethod() //cannot override inherited member because it is sealed
//{
//}
}
}
In ChildClass we can access the SealedMethod but we can not access the PrivateMethod in the above example.
No comments:
Post a Comment
This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.