This article is part of an interview question series that includes various topics with explanations and possible questions based on the topic. This article explains the concept of method overloading and method overriding.
Method overloading
Having methods of the same name and different signatures in scope, or we can say in a class, is known as method overloading. The unique signature of a method indicates that either it has a different number of parameters or different data types of parameters.
So, a question might arise, which is how does the class distinguish the methods with the same name? It is distinguished by the method signature.
Purpose of method overloading
- Increases the readability of the code, as different methods performing the same action can have the same name but a different number of arguments or different types of arguments
- To achieve compile-time polymorphism.
Example
- using system;
- class VariousAreas {
- public static void area(int x, int y){
- console.WriteLine(“Area of rectangle: ”, x*y);
- }
- public static void area(double x, int y){
- console.WriteLine(“Area of rectangle with type double: ”, x*y );
- }
- Public static void area(int x){
- console.WriteLine(“Area of Square: ”, x*x)
- }
- Public static void area(double x){
- console.WriteLine(“Area of square with type double: ”, x*x );
- }
- Static void main(string[] args){
- Area(5);
- Area(4.5, 10);
- Area(2, 8);
- Area(2.5);
- }
- }
Area of rectangle with type double: 45
Area of square with type double: 6.25
Method Overriding
Having two or more methods with the same name and signature as method in the parent class is known as method overriding. Method overriding allows us to invoke functions from base class to derived class. So, we can also say that a technique that includes creating a method in the derived class that has the same name and signature as the method in the base class is known as method overriding.
Method overriding is a way by which we can achieve run time polymorphism. Both the override method and the virtual method must have the same access level modifier.
Purpose of method overriding
- To achieve run time polymorphism.
- To change the existing functionalities.
Example
- using System;
- class baseClass {
- public void withoutVirtual(){
- console.WriteLine(“Method of baseclass called”);
- }
- public virtual void printMessage()
- {
- console.WriteLine("printMessage method of Base class");
- }
- }
-
- class derivedClass : baseClass
- {
- Public void withoutOverride(){
- console.WriteLine(“Method of derivedClass called”);
- }
- public override void printMessage ()
- {
- console.WriteLine(" printMessage method of Derived class");
- }
- }
-
- class MainClass {
- public static void Main()
- {
- console.WriteLine(“Example without using virtual and override keywords”);
- baseClass obj = new baseClass();
- obj.withoutVirtual();
-
- obj = new derivedClass();
- obj. withoutOverride();
-
- Console.WriteLine(“Example using virtual and override keywords”)
- derivedClass dc= new derivedClass();
- c.printMessage();
- }
- }
Example without using virtual and override keywords:
Method of baseclass called
Method of baseclass called
Example using virtual and override keywords:
printMessage method of Base class
printMessage method of Derived class
As seen in the example shown above, we can use virtual and override keywords to achieve method overriding.
Virtual keyword is a keyword that allows us to override a method with the same name and signature of the base class in the derived class. Using a virtual keyword helps us achieve late binding in which implementation of the method gets decided at runtime rather than at the time of compilation to the method at which it is pointed.
Static methods cannot have virtual keywords, as virtual keywords are used for calling a function dynamically at runtime whereas static methods are decided at compile-time and the methods are attached with the class names, so we have to go through the classes for static methods.
Override keyword is a keyword that helps us override the virtual methods in the base class. Or in other words, we can say it is used to extend or modify the functionality of the method from the base class in the derived class.
Difference between method overloading and method overriding:
Polymorphism
Polymorphism can be achieved by the concepts explained above. Polymorphism is the most common question in interviews for a software developer. Polymorphism is generally of two types first, compile-time polymorphism, and run-time polymorphism.
Compile-time polymorphism is also known as early binding or static binding and runtime polymorphism is also known as late binding or dynamic binding.
Various questions related to method overloading and method overriding
- What is Polymorphism?
- What is method overloading and method overriding?
- What is the difference between method overriding and method overloading?
- What is early binding and late binding?
- How do you define static binding and dynamic binding?
- Why do we need method overloading?
- What is a virtual method? Why a method is declared as a virtual method?
- Can a method declared as static be overridden?
- Which keyword is used to change the data and behavior of a base class by replacing a member of a base class with a new derived member?
- Which modifier is used when a virtual method is redefined by a derived class?
- Which keyword is used for the base class member, in order for an instance of a derived class to completely take over a class member from a base class?
- What are the advantages of polymorphism?
- What is the difference between the new and override keyword?
- Why can't static methods have virtual keyword?
- When is a method considered as an overloaded method?
- How can we execute the superclass method if it is overridden in the sub-class?
- When can a derived class override a base class member?