Method Overloading And Method Overriding

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
  1. using system;     
  2. class VariousAreas {     
  3.    public static void area(int x, int y){     
  4.       console.WriteLine(“Area of rectangle: ”, x*y);     
  5.    }     
  6.    public static void area(double x, int y){     
  7.       console.WriteLine(“Area of rectangle with type double: ”, x*y  );     
  8.    }     
  9.    Public static void area(int x){     
  10.       console.WriteLine(“Area of Square: ”, x*x)     
  11.    }     
  12.    Public static void area(double x){     
  13.       console.WriteLine(“Area of square with type double: ”, x*x );     
  14.    }     
  15.    Static void main(string[] args){     
  16.       Area(5);     
  17.       Area(4.5, 10);     
  18.       Area(2, 8);     
  19.       Area(2.5);     
  20.    }     
  21. }  
Output
 
Area of Square: 25
Area of rectangle with type double: 45
Area of rectangle: 16
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
  1. using System;    
  2. class baseClass {     
  3.      public void withoutVirtual(){   
  4.           console.WriteLine(“Method of baseclass called”);   
  5.      }   
  6.     public virtual void printMessage()    
  7.     {    
  8.         console.WriteLine("printMessage method of Base class");    
  9.     }    
  10. }    
  11.   
  12. class derivedClass : baseClass    
  13. {    
  14.    Public void withoutOverride(){   
  15.       console.WriteLine(“Method of derivedClass called”);   
  16.    }   
  17.     public override void printMessage ()    
  18.     {    
  19.         console.WriteLine(" printMessage method of Derived class");    
  20.     }    
  21. }    
  22.   
  23. class MainClass {    
  24.     public static void Main()    
  25.     {   
  26.       console.WriteLine(“Example without using virtual and override keywords”);   
  27.       baseClass obj = new baseClass();    
  28.       obj.withoutVirtual();    
  29.   
  30.       obj = new derivedClass();    
  31.       obj. withoutOverride();   
  32.   
  33.       Console.WriteLine(“Example using virtual and override keywords”)   
  34.       derivedClass dc= new derivedClass();   
  35.       c.printMessage();   
  36.     }    
  37. }   
Output
 
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:
 
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? 


Similar Articles