Introduction
You can define polymorphism as the ability of treating related types in the same way. Polymorphism give the base class the ability of defining a set of behaviors that the childe classes can override, so each childe class will redefine how it will respond to the same behavior.
The virtual methods
A virtual is a method in a base class which the childe class can reuse it or redefine and customize its behavior to be appropriate to its functionality.
To create an overridable method that can be overridden by childe class you must define this method as (virtual)
// This method can be overridden by any child class
public virtual void Move()
{
// Do something in the base class
}
Example
using System;
public class Car
{
private string model;
private int currentSpeed;
private int maxSpeed;
public Car()
{
}
// This method can be overridden by any child class
public virtual void Move()
{
// Do something in the base class
Console.WriteLine("Your car is moving now");
}
}
If any childe class want to redefine the virtual method it need to use the override keyword, so if we want to override the Move() virtual method in the base class Car we need to do the following.
public class SportCar : Car
{
public override void Move()
{
// Write a new implementation here
}
}
Implementation of the virtual method
When you override a virtual method from the base class, you can use the default implementation of the base class method. You can do this by using the (base) keyword to call the virtual method which in the base class.
As you can see in the overridden method, we used the base keyword to call the Move method in the base class so we are using now the implementation of the Move() method in the base class.
public override void Move()
{
// The default implementation
base.Move();
}
Example
SportCar sportsCar1 = new SportCar();
// Calling the overridden method
sportsCar1.Move();
The result
Your car is moving now
If you do not want to use the default implementation of the base class method you can remove the (base.Move) form the overridden method and then reimplement the method by writing a new code that belong to it as follow:
SportCar sportsCar1 = new SportCar();
// Calling the overridden method
sportsCar1.Move();
Example
SportCar sportsCar1 = new SportCar();
// Calling the overridden method
sportsCar1.Move();
The result
Sport car is moving now
You can also combine by using the virtual method implementation and the overridden method implementation by keeping the (base.Move) in the overridden method implementation
public override void Move()
{
// Using the default implementation
base.Move();
// Write a new implementation here
Console.WriteLine("Sport car is moving now");
}
Note that the overridden method considered as virtual method to the subclasses of the subclass. So if create a new class that inherit from the SportCar class, you can override the Move() method in at as if it is a virtual method.
Example
public class SportCar : Car
{
public override void Move()
{
// Write a new implementation here
Console.WriteLine("Sport car is moving now");
}
}
public class RedSportCar : SportCar
{
public override void Move()
{
// Using the SportCar.Move() method
base.Move();
}
}
Note. SportCar.Move() method work as a virtual method to the RedSportCar class.
The sealed keyword
You can use the sealed keyword with virtual methods if you want to prevent the subclasses form overridden the base classes methods.
Example
public class SportCar : Car
{
// This method can not be overridden by subclasses
public sealed override void Move()
{
// Write a new implementation here
Console.WriteLine("Sport car is moving now");
}
}
public class RedSportCar : SportCar
{
// Compile-time error
// The SportCar.Move() cannot be overridden
// public override void Move()
// {
// base.Move();
// }
}
The abstract methods
If you create a virtual method in the base class it is up to the subclass to override or not to override it. What if you want to enforce the subclasses to extend the base class members? To do so you need to use abstract methods.
Abstract methods are a method with no implementation, and it is a part of an abstract class. Any subclass that derived from this base abstract class must override the abstract method or there will be a compile time error.
Example
public abstract class Car
{
// The abstract method
// Abstract methods don't have a body
public abstract void Move();
}
public class SportCar : Car
{
// The SportCar class must override the
// abstract method Move().
public override void Move()
{
// Write a new implementation here
Console.WriteLine("Sport car is moving now");
}
}
Note. Only abstract classes can have abstract methods.
Thank you for reading, see you next article.