Virtual vs Sealed vs New vs Abstract in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 14-Aug-2018 17:06:32
Advertisements