Understanding base, virtual, override, and new Keywords in C#

C# is full of amazing features that make Object-Oriented Programming (OOP) powerful and flexible. Among them, the keywords base, virtual, override, and new play a crucial role in inheritance and method overriding.

But wait! If you’re feeling confused about when to use override vs new, or why virtual is needed, don’t worry! I’ll explain everything in the simplest and most beginner-friendly way possible! Let’s dive in. 😃

1️⃣ Understanding the base Keyword 🔍

The base keyword is used when a child class wants to access methods, properties, or constructors of its parent class. Think of it as a way to refer back to the base class.

 

Example: Using base to Call Parent Class Method

				
					using System;

class Animal  
{
    public void MakeSound()  
    {
        Console.WriteLine("Animal makes a sound! 🎵");
    }
}

class Dog : Animal  
{
    public void MakeSound()  
    {
        base.MakeSound();  // Calls the parent class method
        Console.WriteLine("Dog barks! 🐶");
    }
}

class Program  
{
    static void Main()  
    {
        Dog myDog = new Dog();
        myDog.MakeSound();
    }
}
				
			
🖥️ Output
				
					Animal makes a sound! 🎵
Dog barks! 🐶
				
			

✔ Here, base.MakeSound(); calls the Animal class’s method before executing the Dog class’s own logic.

2️⃣ The virtual Keyword – Enabling Method Overriding 🔄

By default, methods in C# cannot be overridden. To allow a method to be overridden in a derived class, you must mark it as virtual in the base class.

 

Example: Declaring a virtual Method

				
					class Animal  
{
    public virtual void MakeSound()  // Marked as virtual
    {
        Console.WriteLine("Animal makes a generic sound!");
    }
}
				
			

virtual tells C# that this method can be overridden in child classes.

3️⃣ The override Keyword – Modifying a Parent Class Method 🎯

Now, let’s see how to override a virtual method using override.

Example: Using override to Change Behavior

				
					class Dog : Animal  
{
    public override void MakeSound()  
    {
        Console.WriteLine("Dog barks loudly! 🐕");
    }
}
				
			

✔ The override keyword modifies the parent class method instead of creating a new one.

 

Full Example with Output
				
					using System;

class Animal  
{
    public virtual void MakeSound()  
    {
        Console.WriteLine("Animal makes a generic sound!");
    }
}

class Dog : Animal  
{
    public override void MakeSound()  
    {
        Console.WriteLine("Dog barks loudly! 🐕");
    }
}

class Program  
{
    static void Main()  
    {
        Animal myAnimal = new Animal();
        myAnimal.MakeSound();  

        Dog myDog = new Dog();
        myDog.MakeSound();  
    }
}
				
			

🖥️ Output

				
					Animal makes a generic sound!
Dog barks loudly! 🐕
				
			

✔ The child class completely replaces the parent method.

4️⃣ The new Keyword – Hiding the Parent Method Instead of Overriding 😲

If you don’t use override but still define a method with the same name in the child class, you should use the new keyword.

🚨 Warning: Unlike override, new hides the parent method instead of replacing it.

 

Example: Using new to Hide a Method

				
					class Animal  
{
    public void MakeSound()  
    {
        Console.WriteLine("Animal makes a generic sound!");
    }
}

class Dog : Animal  
{
    public new void MakeSound()  
    {
        Console.WriteLine("Dog barks differently! 🐶");
    }
}
				
			

✔ The new keyword tells C# this is a new method, not an override.

⚡ override vs new – What’s the Difference?

Feature override new
Requires virtual in base class? ✅ Yes ❌ No
Replaces parent method? ✅ Yes ❌ No (only hides it)
Calls the base method automatically? ❌ No ❌ No
Can still access base method using base.MethodName()? ✅ Yes ✅ Yes

5️⃣ Full Example Showing override vs new

				
					using System;

class Animal  
{
    public virtual void Speak()  
    {
        Console.WriteLine("Animal speaks! 🗣️");
    }
}

class Dog : Animal  
{
    public override void Speak()  
    {
        Console.WriteLine("Dog barks loudly! 🐕");
    }
}

class Cat : Animal  
{
    public new void Speak()  
    {
        Console.WriteLine("Cat meows softly! 🐱");
    }
}

class Program  
{
    static void Main()  
    {
        Animal myDog = new Dog();
        myDog.Speak();  // Calls overridden method in Dog

        Animal myCat = new Cat();
        myCat.Speak();  // Calls base method in Animal (not Cat)
    }
}
				
			
🖥️ Output
				
					Dog barks loudly! 🐕
Animal speaks! 🗣️
				
			

What happened? 🤔

Dog used override, so myDog.Speak(); calls the overridden method.
Cat used new, so myCat.Speak(); calls the base method (not the Cat method!).

🚀 Real-World Example: Employee System

Imagine you’re building a system for employees where:

  • All employees have a Work() method.
  • Developers should override it.
  • Interns should hide it (but still have their own version).
				
					class Employee  
{
    public virtual void Work()  
    {
        Console.WriteLine("Employee is working.");
    }
}

class Developer : Employee  
{
    public override void Work()  
    {
        Console.WriteLine("Developer is coding! 💻");
    }
}

class Intern : Employee  
{
    public new void Work()  
    {
        Console.WriteLine("Intern is learning! 📚");
    }
}
				
			

Developer overrides the method, so it’s a real replacement.
Intern hides it with new, so it’s treated differently.

🎯 Final Summary

Use base when calling a method from the parent class.
Use virtual in the base class to allow method overriding.
Use override to modify the method in the child class.
Use new if you want to hide the base method instead of overriding it.

 

🚀 Next What?

Awesome! 🎉 You just learned about the base, virtual, override, and new keywords in C#. Now, you know how to use them to control method behavior in inheritance. Feels great, right?

But wait, there’s more! 🚀

In the next chapter, we’ll dive into Types of Inheritance in C#. You’ll learn about single, multilevel, and hierarchical inheritance with super easy examples. Trust me, it’s going to make inheritance even clearer!

So, ready for the next step? Let’s go! ➡️🔥

Leave a Comment

Share this Doc

base, virtual, override, and new keyword

Or copy link