C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have discussed
fields in C# and also seen some modifiers which are allowed by fields. Let's discuss
abstract,
sealed and
override modifiers in this chapter.
Objective
The main objective of this article is to learn
abstract
,
sealed
and
override
modifiers in
C# programming.
Modifiers in C#
In order to change the declaration of any types and its members (
variables
etc.),
modifiers are used. We have already seen some modifiers which are allowed by
fields in the
previous chapter. Now let's discuss the remaining modifiers one by one. A
bstract classes cannot be instantiated and may have the abstract methods.
Abstract:
The name itself indicates that it doesn't have complete implementations (means abstract). An
abstract class contains all the
abstract methods. All the implementations are done from the
derived classes (
classes inheriting abstract class).
Abstract method is a method without any body, curly braces (
{}
) etc. it simply ends with semicolon (
;
).
For example,
public abstract int Find(); // abstract method...
- Declaration of abstract methods is limited only to the abstract class.
- Override method is used to provide implementation of abstract method.
- We cannot modify an abstract class with sealed modifier.
- We cannot use static or virtual modifiers to declare abstract methods.
- Thereby abstract classes are used to create templates for the classes derived from it.
Let's take an example code to find the area of a circle using abstract modifiers,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
abstract class Draw // abstract class
{
public abstract double findArea(); // abstract method
}
class Circle : Draw
{
double radius = 0;
public Circle(double r)
{
radius = r;
}
public override double findArea() // calling abstract method
{
return Math.PI * radius * radius;
}
}
static void Main()
{
Circle c1 = new Circle(5);
Console.WriteLine("Area = " + c1.findArea()); // Area = 78.539.....
Circle c2 = new Circle(7);
Console.WriteLine("Area = " + c2.findArea()); // Area = 153.938....
}
}
}
In the above code, we have an abstract class '
Draw
' with an abstract method '
findArea()
'. We can use that
abstract method by inheriting the
abstract class like in the above code.
Press Ctrl + F5 to get the area of the circle,
sealed:
sealed is a C# keyword which is used to seal a class and prevent it to get inherited from any other classes. In other words, a sealed class cannot be inherited. Example,
sealed class Box // sealed class
{
........; // methods or members cannot be inherited
.......;
}
If you try to inherit this class like below,
class Pen : Box // compile time error
{
......;
.....;
}
It will throw a compile time error as we are going to inherit a sealed class.
In a base class, sealed modifiers can be used on method or function that
overrides a
virtual method. This will allow us to derive the functions from the base class and preventing that specific virtual method.
Take a look at the below example code, which demonstrates the above figure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
class One
{
protected virtual void First()
{
Console.WriteLine("First method in class One");
}
protected virtual void Second()
{
Console.WriteLine("Second method in class One");
}
}
class Two : One
{
sealed protected override void First() // Sealed here
{
Console.WriteLine("First method in class Two");
}
protected override void Second()
{
Console.WriteLine("Second method in class Two");
}
}
class Three : Two
{
protected override void First() // This method is not allowed and gives you compile time error
{
Console.WriteLine("First method in class Three");
}
// We can override Second method.
protected override void Second()
{
Console.WriteLine("Second method in class Threes");
}
}
}
}
In the above code, we have three
inheritance stages of classes. We have two
virtual methods in class
One and the same are inherited by class
Two from there to class
Three.
But, in the class Two, we are sealing the 'First()
' method by using sealed keyword which doesn't allows class Three to inherit that method.
Note: Please read the comments in the code.
The above code will give you the compile time error as we are trying to get a sealed method in class Three from class Two.
override:
In order to modify or extend any virtual or abstract class, override modifier is needed. It provides a new color (implementation) to the virtual or abstract methods which are inherited from the base class.
In the above two code snippets, we have used override modifier to implement the methods from base class to derived class.
- The method used override declaration is known as overridden base method.
- Non-virtual and static methods cannot be overridden.
- Override modifier doesn't effect the accessibility of virtual method as they both have same access level.
- We cannot modify an override method by using
static
, virtual
or new
modifiers. - Overriding declaration must have same
access
modifier
, name
and type
as the base property.
Conclusion
In this article, we have discussed with
abstract,
sealed and
override modifiers in
C#. Hope you understand.
Thanks for reading.
Regards,
Krishna.