Abstract and sealed concept in C#

In this blog we will discuss about abstract and sealed concepts in C#(i.e) abstract classes and methods and also about sealed class and methods.

  • In a number of hierarchical applications, we would have one base class and a number of different derived class.
  • The top most base class simply acts as a base for others and it is of no use of it’s own.
  • In such cases, we might not want to create objects. We can do this by making the base class as abstract.
  • The abstract is a modifier and when used to declare a class indicates that the class cannot be instantiated..
  • Only the derived classes that are not marked as abstract can be instantiated(meaning objects can be created)

Example:

abstract class Base
{ 
  public int Add( int a, int b)
  {
    return(a+b);
  }
}
class Derived : Base
{
  public int mul (int a,int b)
  {
     return(a*b);
  }
}
class test
{
  public static void Main()
  {
    Derived d = new Derived ();
    int result = d.add(10,20);
    Console.WriteLine("value is" + result);
  }
 }
}

output

value is 30

We cannot create objects of base type but we can derive it’s subclass which can be instantiated(meaning object can be created). Some characteristics of abstract classes are listed below:

  • It can be instantiated directly.
  • It can have abstract members.
  •  We cannot apply a sealed modifier to it, which we will be discussing below.

Abstract methods in C#

  • Similar to abstract classes,we can also create abstract methods.
  • An abstract method implicitly is a virtual method and does not provide any implementation.Therefore, an abstract method does not have any method body.Example

public abstract void Draw( int x, int y );

Note that the method body contains only the the semicolon.

Some characteristics of abstract methods are listed below:

  • It cannot have impementation
  • It’s implementation must be provided in non-abstract derived class  by overriding the method.
  • It can be declared only in abstract classes.
  • It cannot be either virtual or static modifiers.

code:

abstract class Base
{
  public abstract int add( int x,int y);
}
Class Derived:Base
{
  public override  int add(int x,int y)
   {
        return x+y;
   }
  public int mul( int a,int b)
  {
       return x*y;
  }
}
Class Main
{
    public static void Main()
    {
        Derived d = new Derived();
        int addition = d.add(2,2);
        int multiplication = d. mul ( 2,4);
        Console.WriteLine("Console.WriteLine("value of addition" + addition" + addition);
        Console.WriteLine("value of multiplication" + multiplication);
    }
}

output:

value of addition 4
value of addition 8

 


Sealed classes in C#

  • The main use of sealed class is to prevent inheritance.
  • Sometimes, we may like to prevent a class being further inherited for security reasons.
  • A class that cannot be inherited is known as Sealed class.
  • This is achieved in c# using a modifier called sealed
sealed class A
{

}
class B:A   // this line will throw an error because class A is defined as sealed so we cannot inherit it.
{
}

  • A sealed class cannot be an abstract class as well.

Sealed methods in C#

  • When a method declaration includes a sealed modifier it is known as a sealed method.
  • This means that the derived class cannot override the base class  method.
  • A sealed method is used to override an inherited virtual method with the same signature(name).
  • That means, the sealed modifier is always used in combination with the override modifier,

Example

class A
{
  public virtual void Fun()
   {

   }
}
class B:A
{  
   public sealed override void Fun()
    {

    }
}



  • The sealed method Fun() overrides the virtual method Fun() defined in class A
  • Any derived class of B cannot further override the method Fun().

This is all about the Abstract and sealed concept in C#.

Leave a Comment

Your email address will not be published. Required fields are marked *