Function overriding in an important concept in OOPS(Object Oriented Programming Concepts). In many technical interviews for Software Developer role, an interviewer will definitely ask questions on function overriding to test the basic understanding of the Object Oriented Programming concepts.
Straight forward interview questions on function overloading are below.
- What is function overriding?
- What is the significance of function overriding in OOPS and where its useful?
- How can we achieve function overriding in .NET?
- Ploymorphism using function overriding? Will it be of compile time or runtime polymorphism?
what is function overriding?
Consider a function defined in a base class. The base class will offer some generic functionality which could be useful for its derived classes in terms of methods. In case if any of the derived classes want a different/custom behavior for the method, they can do so by overriding the method. Optionally still they want base class behavior, they can do so by calling the base class implementation in their custom implementation of the method.
What is not allowed while function overriding?
Method/function signature should be same(including the method name, parameters and the return type). If there is any variation in the method signature in the derived class implementation, it will not lead to function overloading.Let's take an C# example
{
class Program
{
static void Main(string[] args)
{
// doctor variable refers to instance or
// object of base Doctor class.
Doctor doctor = new Doctor();
doctor.TreatPatient();
// doctor variable refers to instance or
// object of derived Surgeon class.
doctor = new Surgeon();
doctor.TreatPatient();
Console.Read();
}
}
public class Doctor
{
public virtual void TreatPatient()
{
Console.WriteLine("Doctor class:
I diagnose patients and prescribe medicine.");
}
}
public class Surgeon : Doctor
{
// Override the behavior of the base class and customize it.
public override void TreatPatient()
{
base.TreatPatient();
Console.WriteLine("Surgeon class: I do surgery.");
}
}
}
Lets look at the output of the program.
Doctor class: I diagnose patients and prescribe medicine.
Doctor class: I diagnose patients and prescribe medicine.
Surgeon class: I do surgery.
Now let's start analyzing the output.
Doctor class is the base class. It has a method called TreatPatient() which is marked as virtual. When a method is marked as virtual, it means the derived classes can override the method, so that they can add their custom implementation for the method.
Surgeon class is the derived class inherited from the base class Doctor. It overrides the TreatPatient() method and adds its own required implementation and it finds the base class implementation of the TreatPatient() method is still needed, so it makes a call to the same.
doctor.TreatPatient();
Since the doctor variable refers to instance of the Doctor class, making a call to TreatPatient() method will execute the base class method.
doctor.TreatPatient();
Now the doctor variable refers to derived Surgeon lass instance, making a call to TreatPatient() method will execute the derived class method.
Depending on which class instance of class the doctor variable (of type Doctor, the base class) refers to, the respective implementation of the method is called. Here the variable is same and the method called is same, but a different behavior is shown. So it exhibits the polymorphism. Since it happens during the execution of the program (i.e. assigning an instance to the variable), this is referred as runtime polymorphism.
If you want to know more about runtime and compile time polymorphism, refer my other post, Compile time and run-time polymorphism
Download: The sample code used in the post is available as a project.
You may also interested in, blog post on Function overloading in OOPS