DEV Community

Cover image for Sealed v/s Abstract Classes
Sukhpinder Singh
Sukhpinder Singh

Posted on • Updated on • Originally published at singhsukhpinder.Medium

Sealed v/s Abstract Classes

The article demonstrates the use of abstract and sealed class

Learning Objectives

  • What is a Sealed class

  • What is an Abstract class

Prerequisites

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.
}
Enter fullscreen mode Exit fullscreen mode

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() { } 
}
Enter fullscreen mode Exit fullscreen mode

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. 
}
Enter fullscreen mode Exit fullscreen mode

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); 
}
Enter fullscreen mode Exit fullscreen mode

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.
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Buy Me A Coffee

Top comments (0)