Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Virtual vs Sealed vs New vs Abstract in C#
In C#, virtual, sealed, new, and abstract are important keywords that control method inheritance and overriding behavior. Understanding these modifiers is crucial for implementing proper object-oriented design patterns and controlling how classes interact through inheritance.
Virtual
The virtual keyword allows a method in a base class to be overridden in derived classes. When a method is declared as virtual, derived classes can provide their own implementation using the override keyword.
Syntax
public virtual ReturnType MethodName() {
// base implementation
}
Example
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks");
}
}
class Program {
public static void Main() {
Animal animal = new Animal();
Animal dog = new Dog();
animal.MakeSound();
dog.MakeSound();
}
}
The output of the above code is −
Animal makes a sound Dog barks
Sealed
The sealed keyword prevents inheritance. When applied to a class, it cannot be inherited. When applied to a method, it prevents further overriding in derived classes. A sealed method must be an overridden method in a derived class.
Syntax
public sealed class ClassName { }
public sealed override void MethodName() { }
Example
using System;
class Animal {
public virtual void Move() {
Console.WriteLine("Animal moves");
}
}
class Mammal : Animal {
public sealed override void Move() {
Console.WriteLine("Mammal walks");
}
}
class Dog : Mammal {
// Cannot override Move() here - it's sealed in Mammal
public void Bark() {
Console.WriteLine("Dog barks");
}
}
class Program {
public static void Main() {
Dog dog = new Dog();
dog.Move();
dog.Bark();
}
}
The output of the above code is −
Mammal walks Dog barks
New
The new keyword hides a base class member in the derived class. This is called method shadowing or method hiding. Unlike overriding, the base class method is not replaced − it's simply hidden when accessed through the derived class.
Syntax
public new ReturnType MethodName() {
// new implementation that hides base method
}
Example
using System;
class BaseClass {
public void Display() {
Console.WriteLine("Base class method");
}
}
class DerivedClass : BaseClass {
public new void Display() {
Console.WriteLine("Derived class method");
}
}
class Program {
public static void Main() {
DerivedClass obj = new DerivedClass();
BaseClass baseObj = new DerivedClass();
obj.Display(); // Calls derived method
baseObj.Display(); // Calls base method
}
}
The output of the above code is −
Derived class method Base class method
Abstract
The abstract keyword creates abstract classes and methods. Abstract methods have no implementation and must be overridden in derived classes. Abstract classes cannot be instantiated directly.
Syntax
public abstract class ClassName {
public abstract void MethodName();
}
Example
using System;
abstract class Vehicle {
public abstract void Start();
public void Stop() {
Console.WriteLine("Vehicle stopped");
}
}
class Car : Vehicle {
public override void Start() {
Console.WriteLine("Car engine started");
}
}
class Program {
public static void Main() {
Car car = new Car();
car.Start();
car.Stop();
}
}
The output of the above code is −
Car engine started Vehicle stopped
Comparison
| Keyword | Purpose | Can be overridden | Must have implementation |
|---|---|---|---|
| Virtual | Allows overriding in derived classes | Yes | Yes |
| Sealed | Prevents further inheritance/overriding | No | Yes |
| New | Hides base class member | No (hides instead) | Yes |
| Abstract | Forces implementation in derived class | Must be overridden | No |
Conclusion
These four keywords provide different levels of control over inheritance in C#. Use virtual for overrideable methods, sealed to prevent inheritance, new to hide base members, and abstract to enforce implementation in derived classes.
