Sealed v/s Abstract Classes


The article demonstrates the use of abstract and sealed class

Photo by Sabrina Gelbart from Pexels

Learning Objectives

  1. What is a Sealed class

  2. What is an Abstract class


Require a basic understanding of OOPS concepts.

Getting Started

Sealed Class

The sealed keyword permits restriction of the inheritance of a class or its members.

To mark a class as sealed, put the keyword sealed before the class definition. For example:

public sealed class D {
   // Class members here.

Sealed classes prevent derivation as they cannot act as a base class.

Common Usage of Sealed Class

Any method, indexer, property, or event on a derived class overriding a virtual member of the base class can declare that member sealed. It negates the virtual aspect of the member for any further inheritance. Simply putting a sealed keyword before the override keyword in the class member declaration. For example:

public class D : C {     
   public sealed override void Do() { } 

Abstract Class

The abstract keyword permits creating classes and members that are incomplete and needs implementation in a derived class.

To mark a class as abstract, put the keyword abstract before the class definition. For example:

public abstract class A {     
   // Class members here. 

An abstract class cannot be initialized, i.e., no object creation is allowed for that class. An abstract class aims to provide a standard definition of a base class that multiple derived classes can share via inheritance.

Abstract classes can also consist of abstract methods. Accomplished by adding the keyword abstract before the return type of the method. For example:

public abstract class A {     
    public abstract void Do(int i); 

Common Usage of Abstract Class

For example, a class lib defines an abstract class used as a parameter to many of its functions and requires programmers to use that lib to provide their implementation by creating a derived class.

Abstract v/s virtual Use Case

Abstract methods also have no implementation, so the method is followed by a semicolon. Derived classes of the abstract class must complete all abstract methods. But when an abstract class inherits a virtual method from a base class, the derived abstract class can override the virtual with an abstract method. For example:

public class D
    public virtual void Do(int i)
        // Original implementation.
public abstract class E : D
    public abstract override void Do(int i);
public class F : E
    public override void Do(int i)
        // New implementation.

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on LinkedIn Instagram Facebook Twitter

#Csharp #Oop #DotnetCore #Dotnet #OopsConcepts

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w