C# > Object-Oriented Programming (OOP) > Inheritance > Method Overriding with virtual and override
Virtual and Override: Extending Functionality Through Inheritance
This code snippet demonstrates how to use the virtual
and override
keywords in C# to achieve polymorphism through inheritance. We'll define a base class with a virtual method, allowing derived classes to provide their own specific implementation.
The Base Class: Animal
The Animal
class is our base class. The MakeSound()
and GetDescription()
methods are declared as virtual
. This means that derived classes *can* override these methods with their own implementations, but they don't *have* to. If a derived class doesn't override a virtual method, the base class's implementation will be used.
public class Animal
{
public virtual string MakeSound()
{
return "Generic animal sound";
}
public virtual string GetDescription()
{
return "This is a generic animal.";
}
}
The Derived Class: Dog
The Dog
class inherits from the Animal
class. The MakeSound()
and GetDescription()
methods are overridden using the override
keyword. This tells the compiler that we are intentionally replacing the base class's implementation with our own. Now, when MakeSound()
or GetDescription()
is called on a Dog
object, the Dog
's implementation will be executed.
public class Dog : Animal
{
public override string MakeSound()
{
return "Woof!";
}
public override string GetDescription()
{
return "This is a dog, a loyal and friendly companion.";
}
}
The Derived Class: Cat
The Cat
class, similarly to the Dog
class, inherits from Animal
and overrides the MakeSound()
and GetDescription()
methods to provide its own specific sound.
public class Cat : Animal
{
public override string MakeSound()
{
return "Meow!";
}
public override string GetDescription()
{
return "This is a cat, an independent and curious creature.";
}
}
Putting it Together: Demonstration
This Main
method creates instances of Animal
, Dog
, and Cat
. When MakeSound()
or GetDescription()
is called on each object, the appropriate implementation is executed based on the object's type. The output will demonstrate that each animal makes its own sound.
public class Program
{
public static void Main(string[] args)
{
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();
Console.WriteLine("Animal sound: " + animal.MakeSound());
Console.WriteLine("Dog sound: " + dog.MakeSound());
Console.WriteLine("Cat sound: " + cat.MakeSound());
Console.WriteLine("Animal description: " + animal.GetDescription());
Console.WriteLine("Dog description: " + dog.GetDescription());
Console.WriteLine("Cat description: " + cat.GetDescription());
}
}
Output
This is the expected output of the program. It clearly shows how the overridden methods provide different behaviors for different derived classes.
Animal sound: Generic animal sound
Dog sound: Woof!
Cat sound: Meow!
Animal description: This is a generic animal.
Dog description: This is a dog, a loyal and friendly companion.
Cat description: This is a cat, an independent and curious creature.
Concepts Behind the Snippet
MakeSound()
method behaves differently depending on the type of the object it's called on.virtual
in the base class can be overridden by derived classes.
Real-Life Use Case
Consider a drawing application. You might have a base class called Shape
with a Draw()
method. Derived classes like Circle
, Square
, and Triangle
could then override the Draw()
method to implement their specific drawing logic. This enables a uniform way to handle drawing different shapes without knowing their exact type.
Best Practices
virtual
sparingly: Only make methods virtual if you anticipate that derived classes will need to change their behavior. Overuse can lead to unnecessary complexity.sealed
keyword on a class to prevent further inheritance if you don't want any more classes deriving from it. This can improve performance.override
when overriding: This makes your code more explicit and helps the compiler catch errors if you're not actually overriding a virtual method.
Interview Tip
Be prepared to explain the difference between virtual
, override
, and abstract
methods. Also, understand the benefits of using inheritance and polymorphism, such as code reusability and extensibility.
When to Use Them
Use virtual
and override
when you have a base class that defines a general behavior, but you want derived classes to be able to customize that behavior without completely rewriting the method. This is particularly useful when dealing with collections of objects where you need to perform a similar action on each object, but the details of the action depend on the object's specific type.
Memory Footprint
Virtual methods incur a slight performance overhead because the runtime needs to look up the correct method to call at runtime (dynamic dispatch). However, this overhead is usually negligible compared to the benefits of polymorphism. The virtual method table (vtable) adds to the memory footprint of the class.
Alternatives
virtual
and override
, they provide an alternative way to achieve polymorphism.
Pros
Cons
FAQ
-
What is the difference between
virtual
andabstract
methods?
virtual
methods have a default implementation in the base class and can be overridden in derived classes.abstract
methods have no implementation in the base class and *must* be implemented in derived classes. An abstract class cannot be instantiated. -
What happens if I don't use the
override
keyword when overriding a virtual method?
The compiler will usually issue a warning, indicating that you are hiding the base class member instead of overriding it. The derived class will effectively have two methods with the same name: the hidden base class method and the new method defined in the derived class. This can lead to unexpected behavior. -
Can I override a method multiple times in different levels of the inheritance hierarchy?
Yes, you can override a virtual method at each level of the inheritance hierarchy. The most derived class's implementation will be used when the method is called.