Skip to Content
All posts

Understanding virtual, override, and method overloading in C#

2 min read ·  — #csharp-interview#junior#oop

Understanding virtual, override, and method overloading in C#

1. Virtual and Override

The virtual keyword in C# allows subclasses to override methods of the base class. This is critical for enabling polymorphism in object-oriented programming.

For example, consider an Animal base class with a virtual Speak method:

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("The animal makes a sound.");
    }
}

The Speak method can be overridden in a Dog class:

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("The dog barks.");
    }
}

This override means that if you create an instance of Dog and call its Speak method, you'll get "The dog barks" instead of "The animal makes a sound".

Dog dog = new Dog();
dog.Speak();  // Output: The dog barks.

But if you call the Speak method through a variable of type Animal, you still get the overridden version:

Animal animal = new Dog();
animal.Speak();  // Output: The dog barks.

This is called polymorphism: the same method call can have different effects depending on the type of object it's called on.

2. Method Overloading

Method overloading in C# allows a class to have multiple methods with the same name, but with different parameters. Overloaded methods can have different numbers of parameters, or parameters of different types.

For instance, let's consider a Print method which could print integers or strings:

public class Printer
{
    public void Print(int i)
    {
        Console.WriteLine("Printing an integer: " + i);
    }

    public void Print(string s)
    {
        Console.WriteLine("Printing a string: " + s);
    }
}

You can call these methods like so:

Printer printer = new Printer();
printer.Print(5);  // Output: Printing an integer: 5
printer.Print("Hello");  // Output: Printing a string: Hello

Method overloading is useful because it allows you to provide the same functionality for different types or quantities of arguments, making your code easier to read and use.

3. Implementing Interface Methods

Interfaces in C# define a contract that classes can implement. An interface can declare any number of methods, properties, events, or indexers. When a class implements an interface, it commits to providing an implementation for each of these members.

Let's create an IFlyable interface and a Bird class that implements it:

public interface IFlyable
{
    void Fly();
}

public class Bird : IFlyable
{
    public void Fly()
    {
        Console.WriteLine("The bird flies in the sky.");
    }
}

Now we can create a Bird instance and call its Fly method:

IFlyable flyable = new Bird();
flyable.Fly();  // Output: The bird flies in the sky.

This example shows that a Bird can fly because it implements the IFlyable interface. If we had other classes like Airplane or Superhero that could also fly, they could implement IFlyable as well. This is an important principle of OOP known as "programming to an interface, not an implementation".

Conclusion

Understanding these concepts is key to properly utilizing C# in an object-oriented manner. They enable you to create more flexible and maintainable code, and are thus important topics in technical interviews.

References: