Difference Between Method Overloading And Method Overriding

In this article, I will demonstrate the difference between method overloading and method overriding with examples that will help you to understand step by step in a better way. Method overloading and method overriding is the most important concept of object-oriented programming. When you are appearing in an interview, you may face this question in different ways from the interviewer. The interviewer may ask about this concept in different ways as below

  1. Explain polymorphism concept
  2. Static and dynamic polymorphism
  3. compile time and run time polymorphism
  4. Early binding and Late binding

For the answer to all four questions, you need to explain method overloading and method overriding with examples. Let's make clear which one is called Overloading and Overriding.

Static/Compile time polymorphism/early binding  → Method overloading

Dynamic/run time polymorphism/late binding → Method overriding

Method Overloading

Method overloading is the ability to have more than one method in the same class with the same name but different number of parameters or parameters with different data types. All method will have their own separate implementation and different logic. The overloaded method can call based on the passed parameter.

Now let's take an example of Static/Compile time polymorphism/early binding with an example, so it will help us to understand step by step. Method overloading means a method with the same signature, but parameters may differ. While doing method overloading, you must have the same return type for all overloaded methods. Method overloading, we need to keep in mind that changing only the return type of method will not make the method as method overload. Let's take an example that will help us to get a better idea of method overloading.

namespace OverloadingAndOverriding {
    class Program {
        static void Main(string[] args) {
            Employee employee = new Employee();
            var allEmployees = employee.GetEmployees()
            Console.WriteLine("Hello World!");
        }
    }
    public class Employee {
        public int Id {
            get;
            set;
        }
        public string FirstName {
            get;
            set;
        }
        public string LastName {
            get;
            set;
        }
        public string Email {
            get;
            set;
        }
        public int Mobile {
            get;
            set;
        }
        public string City {
            get;
            set;
        }
        public List < Employee > GetEmployees() {
            // This method will return all Employees
            var employees = GetEmployeesFromDB();
            return employees;
        }
        public List < Employee > GetEmployees(string employeeFirstName) {
            // This method will return all Employees whose first name match with parameter passed
            var employees = GetEmployeesFromDB().Where(x => x.FirstName == employeeFirstName).ToList();
            return employees;
        }
        public List < Employee > GetEmployees(string employeeFirstName, string city) {
            // This method will return all Employees whose first name and city match with parameter passed
            var employees = GetEmployeesFromDB().Where(x => x.FirstName == employeeFirstName && x.City == city).ToList();;
            return employees;
        }
        // Created method to get data from database table Employee
        private static List < Employee > GetEmployeesFromDB() {
            //Logic to get employee from database table
            var lstEmpolyee = new List < Employee > ();
            lstEmpolyee.Add(new Employee() {
                Id = 1,
                    FirstName = "param",
                    LastName = "R",
                    Email = "[email protected]",
                    City = "Rajkot",
                    Mobile = 987654321
            });
            lstEmpolyee.Add(new Employee() {
                Id = 2,
                    FirstName = "Vraj",
                    LastName = "S",
                    Email = "[email protected]",
                    City = "Ahmedabad",
                    Mobile = 987654321
            });
            lstEmpolyee.Add(new Employee() {
                Id = 3,
                    FirstName = "Vihar",
                    LastName = "S",
                    Email = "[email protected]",
                    City = "Ahemdabad",
                    Mobile = 987654322
            });
            lstEmpolyee.Add(new Employee() {
                Id = 4,
                    FirstName = "Riyan",
                    LastName = "S",
                    Email = "[email protected]",
                    City = "Surat",
                    Mobile = 987654323
            });
            lstEmpolyee.Add(new Employee() {
                Id = 5,
                    FirstName = "Vivan",
                    LastName = "V",
                    Email = "[email protected]",
                    City = "Jamnagar",
                    Mobile = 987654324
            });
            lstEmpolyee.Add(new Employee() {
                Id = 6,
                    FirstName = "Jaitik",
                    LastName = "A",
                    Email = "[email protected]",
                    City = "AMD",
                    Mobile = 987654325
            });
            return lstEmpolyee;
        }
    }
}

Let's check while calling overloading methods from the Main method. You will be able to see all three overloaded methods in the below snippet with red color marked text. First method called without any parameter, the second time called with a single string parameter, and the third time call I passed two string parameters. Based on the passed parameter, the compiler will identify and call the respective method which matches with passed parameter.

Difference between method overloading and method overriding

Method Overriding

Method overriding means the ability to override the parent class method into the child class, Developer can write a method with the same name and parameter in the parent and child class by marking with virtual and override keywords. It means you can redefine your method in the derived class. Parent class method should be marked with virtual and child class method should be marked with the override keyword. It means you have the same method with the same parameter, and you can write different logic in the base and derived classes. Let's take an example of method overriding,

public class BaseClass {
    public virtual string GreetingMessage() {
        return "Greeting from Base class";
    }
}
public class ChildClass: BaseClass {
    public override string GreetingMessage() {
        return "Greeting from Child class";
    }
}
class Program {
    static void Main(string[] args) {
        BaseClass baseClass = new BaseClass();
        Console.WriteLine(baseClass.GreetingMessage());
        ChildClass childClass = new ChildClass();
        Console.WriteLine(childClass.GreetingMessage());
        BaseClass baseForchildClass = new ChildClass();
        Console.WriteLine(baseForchildClass.GreetingMessage());
        Console.ReadLine();
    }
}

The output of above code snippet,

Difference between method overloading and method overriding

Summary

Method Overloading

It means defining more than one method with the same method name and a different number of parameters or parameters with different data types. 

Method Overriding

It means the ability to redefine the method of the parent class in the child class with the same method name and parameters. In method overriding parent class method must be marked with the "Virtual" keyword and the child class method must be marked with the "override" keyword. 


Recommended Free Ebook
Similar Articles