Can You Assign a Derived Class Object to a Base Class Variable? Which Method Will Be Invoked?
If you're learning object-oriented programming (OOP), you might have come across this common interview question: "Can you assign an object of a derived class to a variable of the base class, and if both have the same method name, which method will be invoked?" Let’s break this down step by step with clear explanations and examples.
The Short Answer
Yes, you can assign an object of a derived class to a variable of the base class. This is a fundamental concept in OOP called polymorphism. However, when both the base class and derived class have a method with the same name, the method that gets invoked depends on whether the method is overridden in the derived class or simply hidden. Let’s explore this in detail.
Understanding the Basics
1. Base Class and Derived Class
- A base class (or parent class) is a general class that defines common properties and behaviors.
- A derived class (or child class) inherits from the base class and can add or modify its behaviors.
2. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common base class. This is useful for writing flexible and reusable code.
3. Method Overriding vs. Method Hiding
- Method Overriding: The derived class provides a new implementation for a method that is already defined in the base class. This is done using the
override
keyword in C#.
- Method Hiding: The derived class defines a method with the same name as the base class method but does not override it. Instead, it hides the base class method. This is done using the
new
keyword in C#.
Example in C#
Let’s look at an example to understand how this works.
Base Class and Derived Class
public class BaseClass
{
public void CallMe()
{
Console.WriteLine("Base class method invoked.");
}
}
public class ChildClass : BaseClass
{
public new void CallMe()
{
Console.WriteLine("Child class method invoked.");
}
}
Here:
BaseClass
has a method called CallMe
.
ChildClass
inherits from BaseClass
and defines its own CallMe
method using the new
keyword. This means it hides the base class method instead of overriding it.
Assigning Derived Class Object to Base Class Variable
Now, let’s create an instance of ChildClass
and assign it to a variable of type BaseClass
:
BaseClass obj = new ChildClass();
obj.CallMe();
Output
Base class method invoked.
Why Does This Happen?
When you assign a ChildClass
object to a BaseClass
variable, the compiler treats the object as if it’s of type BaseClass
. Since the CallMe
method in ChildClass
is hidden (not overridden), the base class method is invoked.
What If We Override the Method?
If you want the derived class method to be invoked, you need to override the base class method. Here’s how you can do it:
Modified Example with Method Overriding
public class BaseClass
{
public virtual void CallMe()
{
Console.WriteLine("Base class method invoked.");
}
}
public class ChildClass : BaseClass
{
public override void CallMe()
{
Console.WriteLine("Child class method invoked.");
}
}
Here:
- The
CallMe
method in BaseClass
is marked as virtual
, which means it can be overridden.
- The
CallMe
method in ChildClass
is marked as override
, which means it provides a new implementation for the base class method.
Assigning Derived Class Object to Base Class Variable
BaseClass obj = new ChildClass();
obj.CallMe();
Output
Child class method invoked.
Key Takeaways
- Assigning Derived Class to Base Class Variable: You can assign an object of a derived class to a variable of the base class type. This is a core feature of polymorphism.
- Method Invocation:
- If the derived class hides the base class method (using
new
), the base class method is invoked.
- If the derived class overrides the base class method (using
override
), the derived class method is invoked.
- Use
virtual
and override
for Polymorphism: To ensure the correct method is invoked based on the object’s actual type, use virtual
in the base class and override
in the derived class.
Real-Life Analogy
Think of a vehicle as a base class and a car as a derived class. Both have a method called StartEngine
. If the StartEngine
method in the car class hides the base class method, calling it from a vehicle variable will start the engine in a generic way (base class behavior). However, if the car class overrides the method, calling it from a vehicle variable will start the engine in a car-specific way (derived class behavior).
Conclusion
Understanding how method invocation works when assigning a derived class object to a base class variable is crucial for mastering polymorphism in OOP. By using virtual
and override
keywords, you can ensure that the correct method is called based on the object’s actual type. This makes your code more flexible, reusable, and easier to maintain.