Learn something new every day More Info... by email
Method overloading is a feature in most object-oriented programming languages in which two or more methods share the same name but have different parameters. Specifically, the number, data type, and/or order of the parameters are different. When the code is compiled, the correct method will be automatically selected 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 method overloading would be a method that calculates the area of a square. It might be defined as getArea(Square s). This method could be overloaded to additionally calculate the area of the circle by adding the method getArea(Circle c).
The primary requirement for method overloading is that the methods share the same name. Their method signatures — the method name, the number of parameters, and the parameter data types — should otherwise be unique. In 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 an employee object with blank name and dob fields. The second sets the name field, but leaves the dob field blank, and the third defines both the name and dob fields.
Method overloading is most often done to make more than one method appear logically 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 still presenting itself logically as a single method.
Methods are also overloaded to preserve backwards compatibility. A method that does a complex calculation could be given a new requirement to optionally perform the same calculation with a slight change. A new parameter is added to the method that will determine how to perform the calculation — the old way or the new way.
To avoid having to find all cases in which the method is called and add the new parameter, the method can be overloaded. The new method will have the old signature and be called by existing code. It will not contain any logic itself, and will simply call the modified method and pass in a default of “old way” for the new parameter. 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, in which the same logical method can be, in practice, used in multiple ways. Method overloading is not the same as method overriding. Method overriding is where the definition of a method in a parent class is changed by a child class. In this case, both methods will have the same signature.