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
What is the difference between virtual and abstract functions in C#?
In C#, both virtual and abstract methods enable polymorphism but serve different purposes. Virtual methods provide a default implementation that can be overridden, while abstract methods have no implementation and must be overridden by derived classes.
Understanding the difference between these two concepts is crucial for implementing proper inheritance hierarchies and achieving runtime polymorphism in your applications.
Syntax
Following is the syntax for declaring a virtual method −
public virtual returnType MethodName() {
// default implementation
}
Following is the syntax for declaring an abstract method −
public abstract class ClassName {
public abstract returnType MethodName();
}
Virtual Methods
Virtual methods have a default implementation and can exist in both abstract and non-abstract classes. Derived classes can choose to override virtual methods using the override keyword, but it's optional. If not overridden, the base class implementation is used.
Example
using System;
class Shape {
protected int width, height;
public Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
public virtual int Area() {
Console.WriteLine("Parent class area:");
return 0;
}
}
class Rectangle : Shape {
public Rectangle(int a = 0, int b = 0) : base(a, b) {
}
public override int Area() {
Console.WriteLine("Rectangle class area");
return (width * height);
}
}
class Triangle : Shape {
public Triangle(int a = 0, int b = 0) : base(a, b) {
}
public override int Area() {
Console.WriteLine("Triangle class area:");
return (width * height / 2);
}
}
class Program {
static void Main(string[] args) {
Shape s1 = new Rectangle(10, 7);
Shape s2 = new Triangle(10, 5);
Console.WriteLine("Area: {0}", s1.Area());
Console.WriteLine("Area: {0}", s2.Area());
}
}
The output of the above code is −
Rectangle class area Area: 70 Triangle class area: Area: 25
Abstract Methods
Abstract methods have no implementation and must be declared within an abstract class. Derived classes are required to override all abstract methods. This ensures that every concrete implementation provides its own specific behavior.
Example
using System;
public abstract class Vehicle {
public abstract void Display();
public void StartEngine() {
Console.WriteLine("Engine started");
}
}
public class Bus : Vehicle {
public override void Display() {
Console.WriteLine("Bus");
}
}
public class Car : Vehicle {
public override void Display() {
Console.WriteLine("Car");
}
}
public class Motorcycle : Vehicle {
public override void Display() {
Console.WriteLine("Motorcycle");
}
}
public class Program {
public static void Main() {
Vehicle v1 = new Bus();
Vehicle v2 = new Car();
Vehicle v3 = new Motorcycle();
v1.Display();
v1.StartEngine();
v2.Display();
v3.Display();
}
}
The output of the above code is −
Bus Engine started Car Motorcycle
Key Differences
| Virtual Methods | Abstract Methods |
|---|---|
| Have a default implementation | No implementation provided |
| Can exist in any class | Must be in an abstract class |
| Override is optional | Override is mandatory |
| Can be called directly on base class | Cannot instantiate abstract class |
Use virtual keyword |
Use abstract keyword |
Conclusion
Virtual methods provide flexibility with optional overriding and default implementations, while abstract methods enforce a contract requiring all derived classes to provide their own implementation. Choose virtual methods when you want to provide a default behavior that can be customized, and abstract methods when you need to ensure every derived class implements specific functionality.
