Virtual vs Sealed vs New vs Abstract in C#


Virtual

The virtual keyword allows a class to be overridden. For overriding a parent class method in the child class, declare the parent class method as virtual.

Sealed

When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.

To prevent being overridden, use the sealed in C#. When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.

public sealed override void getResult() { }

New

Use the new keyword to hide the base class method from the derived class. This is done by declaring the derived class function with new. This is how new is used in Shadowing.

public new string getResult()

Abstract

Abstract classes contain abstract methods, which are implemented by the derived class.

abstract class Vehicle

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 21-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements