If We Have a Virtual Method in the Base Class and Override It in the Child Class, Which Method Will Be Invoked?
Short Answer
If you have a virtual method in the base class and override it in the child class, and then create an instance of the child class but assign it to a variable of the base class type, the overridden method in the child class will be invoked. This behavior is part of polymorphism and is known as dynamic method dispatch.
Detailed Explanation with Examples
In object-oriented programming, polymorphism allows you to treat objects of derived classes as objects of their base classes. When you override a virtual method in a child class, the actual method that gets called is determined at runtime based on the type of the object, not the type of the reference.
What Happens When You Override a Virtual Method?
When you mark a method as virtual
in the base class, it allows derived classes to provide their own implementation of the method using the override
keyword. If you create an instance of the child class but assign it to a variable of the base class type, the overridden method in the child class will still be called. This is because the method to be executed is determined by the actual object type at runtime, not the reference type.
Example in C#
Let’s look at an example to understand this behavior:
using System;
// Base class with a virtual method
class BaseClass
{
public virtual void SomeMethod()
{
Console.WriteLine("Base class method");
}
}
// Child class that overrides the virtual method
class ChildClass : BaseClass
{
public override void SomeMethod()
{
Console.WriteLine("Child class method");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of ChildClass but assign it to a BaseClass reference
BaseClass instance = new ChildClass();
// Call the method
instance.SomeMethod(); // Output: Child class method
}
}
Output
When you run the above program, the output will be:
Child class method
Explanation of the Code
- Base Class (
BaseClass
): The BaseClass
has a virtual method called SomeMethod
. This method can be overridden by derived classes.
- Child Class (
ChildClass
): The ChildClass
overrides the SomeMethod
method and provides its own implementation.
- Creating an Instance: An instance of
ChildClass
is created but assigned to a variable of type BaseClass
.
- Calling the Method: When
SomeMethod
is called on the instance
, the overridden method in ChildClass
is executed, even though the reference type is BaseClass
.
Why Does This Happen?
This behavior is due to polymorphism and dynamic method dispatch. At runtime, the actual type of the object (ChildClass
) is checked, and the corresponding overridden method is called. This allows you to write flexible and reusable code.
Key Points to Remember
- Virtual Method: A method marked as
virtual
in the base class can be overridden in derived classes.
- Override Keyword: Use the
override
keyword in the child class to provide a new implementation of the virtual method.
- Runtime Decision: The method to be executed is determined at runtime based on the actual object type, not the reference type.
Real-Life Analogy
Think of a vehicle as a base class and a car as a derived class. The base class defines a generic behavior like StartEngine
, but the derived class can override this behavior to start the engine in a specific way. Even if you refer to the car as a vehicle, it will still use the car's specific way of starting the engine.
Conclusion
In summary, when you override a virtual method in a child class and assign an instance of the child class to a base class reference, the overridden method in the child class will be invoked. This is a powerful feature of polymorphism that allows you to write flexible and maintainable code.