[ad_1]
Method overloading is when multiple methods share the same name but have different parameters. This is used to make methods appear as a single logical method and to preserve backward compatibility. Constructors are often overloaded. Method overloading is a type of polymorphism, different from method overriding.
Method overloading is a feature in most object-oriented programming languages where two or more methods share the same name but have different parameters. In particular, the number, data type and/or order of the parameters are different. When the code is compiled, the correct method will be selected automatically based on how it is called. Methods are also known as functions in some programming languages, so method overloading is sometimes referred to as function overloading.
A simple example of a method overload would be a method that calculates the area of a square. It could be defined as getArea(Square s). This method could be overloaded to further calculate the area of the circle by adding the getArea(Circle c) method.
The primary requirement for method overloading is that the methods share the same name. Their method signatures, method name, number of parameters, and parameter data types, would otherwise have to be unique. This way, the compiler can determine which method to execute.
Constructors, the methods used to instantiate objects, are often overloaded. This is done to initialize an object with non-default values. For example, an employee object with two fields (name and date of birth, or dob) might have the following overloaded constructors: Employee(), Employee(name), and Employee(name, dob). The first constructor creates a dependent object with empty name and dob fields. The second sets the name field, but leaves the dob field empty, and the third defines both the name field and the dob field.
Method overloading is often done to make more than one method logically appear as a single method. In the getArea() example, while there are physically two methods, the caller is presented with a single logical getArea() method. In this way, getArea() can be expanded to work on other shapes – triangles, trapezoids and so on – while logically presenting itself as a single method.
Methods are also overloaded to preserve backward compatibility. A method that performs a complex calculation may receive a new requirement to optionally perform the same calculation with a slight modification. A new parameter is added to the method which will determine how to do the calculation: the old way or the new way.
To avoid having to find every case where the method is called and add the new parameter, you can overload the method. The new method will have the old signature and will be called from existing code. It won’t contain any logic itself and will simply call the modified method and switch to the default “old way” for the new parameter. The new code will call the modified method and pass the new parameter with the appropriate value, old way or new way.
Method overloading is a type of polymorphism, where the same logical method can basically be used in multiple ways. Method override is not the same as method override. Method overriding is where the definition of a method in a parent class is modified by a child class. In this case, both methods will have the same signature.