Method overriding

Method overriding is a technique in C# that we can use to invoke the methods that are defined in the base class from the classes that are deriving from that base class. In method overriding, a derived class implements a function that's declared in the base class with the same signature:

  • The same name as the function that's declared in the base class
  • The same number and type of parameters in the function
  • The same return type as the function declared in the base class

 In C#, method overriding is implemented using the following two methods:

  • Virtual methods: A virtual method is a method in the base class that can also be defined or overridden in the derived class as well. Please note that, when a method is declared as virtual, it's optional to define the implementation of the method in the base class. In case it's defined, it becomes even more optional for the derived class to override it further. A method is declared as virtual using the virtual keyword.
  • Override: Once a method has been declared as virtual or abstract in the base class, then by using the override keyword, the derived class can redefine the implementation of the method for its own use. In this section, we will be looking at virtual methods. In the next section, Abstract and sealed classes, we will do a deep dive into abstract methods.

Let's look at a code example to understand how method overriding is implemented in C#. Let's assume that we have a base class, Carand two classes, Ferrari and Suzukithat are inheriting from the Car class. For the sake of explanation, we will keep things simple by just specifying a default constructor and a common Accelerate method across the three classes. The following would be the code implementation for the same:

public class Car
{
public Car()
{
Console.WriteLine("Inside Car");
}
public void Accelerate()
{
Console.WriteLine("Inside Acceleration of Car");
}
}
public class Ferrari : Car
{
public Ferrari()
{
Console.WriteLine("Inside Ferrari");
}
public void Accelerate()
{
Console.WriteLine("Inside Acceleration of Ferrari");
}
}
public class Suzuki : Car
{
public Suzuki()
{
Console.WriteLine("Inside Suzuki");
}
public void Accelerate()
{
Console.WriteLine("Inside Acceleration of Suzuki");
}
}

 Now, let's create some objects for these classes by using the following code:

Car ferrari = new Ferrari();
ferrari.Accelerate();
Console.WriteLine("End of Ferrari Implementation");
Car suzuki = new Suzuki();
suzuki.Accelerate();
Console.WriteLine("End of Suzuki Implementation");

Note that in the preceding code, we have created a new object of the Ferrari class and have assigned it to a variable, ferrari, which is of the Car type. Similarly, we have also created a new object of the Suzuki class and have assigned it to a variable, suzuki, which is also of the Car type.

When we execute the code, we get the following output:

Note that, even though we have the Accelerate method in both the parent Car class and the derived Ferrari and Suzuki classes, when we are calling the Accelerate method from the ferrari object, it's calling the Accelerate method that's present in the parent Car class. This is due to the fact that the type of the variable is Car and that, even though it's instantiated with the objects of the Ferrari and Suzuki child classes, the method in the base class has not been overridden. 

Now let's make a slight change to the implementation and declare the method in the base class as virtual and the methods in the classes deriving from this class as override:

public class Car
{
public Car()
{
Console.WriteLine("Inside Car");
}
public virtual void Accelerate()
{
Console.WriteLine("Inside Acceleration of Car");
}
}
public class Ferrari : Car
{
public Ferrari()
{
Console.WriteLine("Inside Ferrari");
}
public override void Accelerate()
{
Console.WriteLine("Inside Acceleration of Ferrari");
}
}
public class Suzuki : Car
{
public Suzuki()
{
Console.WriteLine("Inside Suzuki");
}
public override void Accelerate()
{
Console.WriteLine("Inside Acceleration of Suzuki");
}
}

Now, execute the same code again and review that we receive the following output:

Note that now, the Accelerate method executes the code mentioned in the derived classes of Ferrari and Suzuki and not the code specified in the parent class of Car.

Later in this chapter, we will also do a deep dive into polymorphism. There are two types of polymorphism: runtime polymorphism and compile-time polymorphism. Runtime polymorphism is implemented using method overriding. 

In the next section, we will look at abstract classes and also explore the use of virtual methods in abstract classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.220