sealed C# with Example



sealed C# with Example

When applied to a class, the sealed modifier prevents other classes from inheriting from it. 
class A { } 
sealed class B : A { } 
class C : B { } //error : Cannot derive from the sealed class 
When applied to a virtual method (or virtual property), the sealed modifier prevents this method (property) from 
being overridden in derived classes. 
public class A 
{ 
public sealed override string ToString() // Virtual method inherited from class Object 
{ 
return "Do not override me!"; 
} 
} 
public class B: A 
{ 
public override string ToString() // Compile time error 
{ 
return "An attempt to override"; 
} 
} 

0 Comment's

Comment Form

Submit Comment