Understanding Sealed and Abstract classes using C#

Sealed and Abstract are keywords in C#. They have a special meaning and difference even though there is a wide similarity between these two terms.

When a base class is declared with sealed keyword, then that class cannot be extended. This is same as final keyword in Java.

Listing 1

public sealed class Computer 
{ 
//code goes here 
} 
class COMPAQ:COMPUTER 
{ 
//Not allowed as base class is sealed 
}


Abstract Classes

Abstract class is a special type of class, which should be declared with abstract keyword. Moreover, it should contain one or more abstract methods, which should contain only method definitions. It won’t be having any method body (in the form of curly braces) like Instance and Static methods.

Normally, a base class is declared with abstract keyword and the derived classes should extend the abstract class and implement relevant methods. Keep in mind that only one abstract class can be extended at a time since C# won’t supports multiple inheritance. Listing 2 illustrates this concept clearly:

Listing 2

using System; 
abstract public class Absdemo 
{ 
public abstract void Show(); 
} 
class Absimp:Absdemo 
{ 
public override void Show() 
{ 
Console.WriteLine("Abstract Method Implemented"); 
} 
public static void Main(string[] args) 
{ 
Absimp ai = new Absimp(); 
ai.Show(); 
} 
} 

										

Leave a Comment