Sealed Classes:
Sealed Classes are the classes that can not be inherited.
Means that it can not act as Base Class.
As it can not act as base class abstract key word cannot be used with sealed class.
The above class can not act as Parent class.
Sealed Methods:
Sealed methods are those overriden methods that can not be further overridden
Only Overridden method can be sealed. Means when a method is there is the child class and we want to further prevent the method from being overridden then we mark the method as sealed along with override.
Sealed Classes are the classes that can not be inherited.
Means that it can not act as Base Class.
As it can not act as base class abstract key word cannot be used with sealed class.
/*------------------------------------------------------------*/
public sealed class Employee
{
private int employeeID;
public int GetEmployeeID()
{
return
employeeID;
}
public void setEmployeeID(int
employeeID)
{
this.employeeID
= employeeID;
}
}
/*------------------------------------------------------------*/
The above class can not act as Parent class.
Sealed Methods:
Sealed methods are those overriden methods that can not be further overridden
Only Overridden method can be sealed. Means when a method is there is the child class and we want to further prevent the method from being overridden then we mark the method as sealed along with override.
/*------------------------------------------------------------*/
class Somesuperclass
{
public virtual void
CheckIsFullTimeorPartTime()
{
// Logic
to check whether the employee is full time or part time.
}
}
class Employee : Somesuperclass
{
public sealed override void CheckIsFullTimeorPartTime()
{
// Logic
to check whether the employee is full time or part time.
}
}
/*------------------------------------------------------------*/
CheckIsFullTimeorPartTime() method has been made as sealed so if further any class will inherit it it will give the error.
/*------------------------------------------------------------*/
class Somesuperclass
{
public virtual void
CheckIsFullTimeorPartTime()
{
// Logic
to check whether the employee is full time or part time.
}
}
class Employee : Somesuperclass
{
public sealed override void CheckIsFullTimeorPartTime()
{
// Logic
to check whether the employee is full time or part time.
}
}
class worker : Employee
{
public override
void CheckIsFullTimeorPartTime()
{
// Error cannot override inherited member because it is sealed.
}
}
/*------------------------------------------------------------*/
No comments:
Post a Comment