How to Override Methods in C#
Method Overriding, in Object-Oriented Programming, is a language feature that allows a child class to implement a method that’s is already is in the parent class. Implementation in the subclass overrides the implementation in base class by creating a method that has the same name.
To understand overriding, let’s look at the real-world example:
Let’s say we have an Animal
class and we have three different animals:
As you can see from the image, those three class; Cat
, Dog
, and Bird
inherit from Animal
class. They all have a bunch of different parameters and methods.
So, let’s take a look at them one by one:
Animal
class have two methods called Eat()
and Move()
. Of course, those three different animals have various type of eating and moving.
Firstly, our Cat
class overrides Eat()
and Move()
method to implement its behavior. Also, it adds its own Meow()
method for its speaking.
Secondly, our Dog
class also overrides Eat()
and Move()
method. Because, the dog has a different type of eating and moving too. It adds its Bark()
method to bark.
Thirdly, our Bird
class overrides Eat()
and Move()
again and a Fly()
method. As everyone knows, only the bird can fly.
As we grasped overriding methods with real-world examples, let’s take a look at actual code.
Overriding Methods in Code
Initially, we create an Animal
class:
class Animal
{
public int age;
public virtual void Eat()
{
// Eat food
}
public virtual void Move()
{
// Move around
}
}
Then, we create three animal class that inherits from Animal
class
class Cat : Animal
{
public new int age;
public override void Eat()
{
// Cat eats food
}
public override void Move()
{
// Cat moves around
}
public void Meow()
{
// Only cats can meow
}
}class Dog : Animal
{
public new int age;
public override void Eat()
{
// Dog eats food
}
public override void Move()
{
// Dog moves around
}
public void Bark()
{
// Only dogs can bark
}
}class Bird : Animal
{
public new int age;
public override void Eat()
{
// Bird eats food
}
public override void Move()
{
// Bird moves around
}
public void Fly()
{
// Only birds can fly
}
}
As you can see there are two different keywords there: virtual and override. Let’s understand why are they used?
Virtual Keyword
To override any method, you need to mark the base class’ method as virtual
as we did with the Animal
class’ method.
Override Keyword
Once we marked our base class’ method as virtual
, we can override it in the child class. To do that, we need to mark our method with override
keyword.
We have learnt the main part, the only thing that left is calling the method we are overriding.
Calling the base class’ method from our child class
Let’s say we have a virtual method that prints “Hello, World!”:
class MainClass
{
public virtual void MainMethod()
{
Console.WriteLine("Hello, World!");
}
}
To override this method, let’s create our child class’ method.
Once you write “public override” to the editor, IntelliSense
will show you the methods that can be overridden. When you click to the method that you want to override, it will automatically write the method code:
class ChildClass : MainClass
{
public override void MainMethod()
{
base.MainMethod();
}
}
Wait a minute, there is a strange code here, base.MainMethod().
Basically, what this method does is calling base the base method that we have overridden. If we now run the program, the output will be like this:
Hello, World!
Also, we can add code before or after base.MainMethod(). Adding code before or after only effect to the calling order. So, let’s print another message that says Hello, C#!.
class ChildClass : MainClass
{
public override void MainMethod()
{
base.MainMethod();
Console.WriteLine("Hello, C#!");
}
}
Now, the output will look like as follow:
Hello, World!
Hello, C#!
Finally, I would mention that overriding method feature is a handy method in Object-Oriented Programming. As we can saw from the examples, we can do so many things by using them. So, I hope you have understood this topic and enjoyed it.