Method overloading

Method overloading

Method overloading is a feature found in various programming languages such as Ada, C#, C++ and Java that allows the creation of several functions with the same name which differ from each other in terms of the type of the input and the type of the output of the function.

An example of this would be a square function which takes a number and returns the square of that number. In this case, it is often necessary to create different functions for integer and floating point numbers.

Method overloading is usually associated with statically-typed programming languages which enforce type checking in function calls. When overloading a method, you are really just making a number of different methods that happen to have the same name. It is resolved at compile time which of these methods are used.

Method overloading should not be confused with ad-hoc polymorphism or virtual functions. In those, the correct method is chosen at runtime.

Constructor overloading

In some object oriented programming languages, constructors can also be overloaded. In languages that require constructors to have the same name as the declaring class, constructor overloading allows multiple ways of creating an object. The constructors must have different parameter types or numbers of parameters.

For example, the class Person below, written in Java, holds data about each person's name and phone number. The constructor is "overloaded" as there are two constructors for the class Person.

public class Person { // instance variables: String name; String phoneNumber;

// Constructor with name and number: public Person(String name, String phoneNumber){ this.name=name; this.phoneNumber=phoneNumber; }

// Alternative constructor without giving phone number. // This will set the phone number to "N/A" (for "not available") public Person(String name){ this(name, "N/A"); } }

In Java, the call this.name=name means "set this object's variable called name to the value of the argument "name".

Note that the second constructor invokes the first one with the call this(name, "N/A"). It must be called on the first line of the constructor and can only be called once per constructor. This is generally considered a good programming practice. It might seem more natural and clear to write the second constructor above as:

public Person(String name){ this.name=name; this.phoneNumber="N/A"; }

This would work the same way as the first example. The benefit of calling one constructor from another is that it provides code that is more flexible. Suppose that the names needed to be stored in lower case only. Then only one line would need to be changed:

this.name=name;

in the first constructor is changed to

this.name=name.toLowerCase();

If the same line was repeated in the second constructor, then it would also need to be changed to produce the desired effect.

ee also

*Factory method pattern
*Object-oriented programming
*Constructor
*Abstraction
*Overriding
*Operator overloading
*Method signature


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Method overriding — Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The… …   Wikipedia

  • Method overriding (programming) — Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides… …   Wikipedia

  • Overloading — See also: Overload The terms overloading and overloaded may refer to: Constructor and function/method overloading, in computer science, a type of polymorphism where different functions with the same name are invoked based on the data types of the …   Wikipedia

  • Method (computer programming) — In object oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property… …   Wikipedia

  • Function overloading — or method overloading is a feature found in various programming languages such as Ada, C#, VB.NET, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and… …   Wikipedia

  • Operator overloading — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

  • Factory method pattern — Factory method in UML Facto …   Wikipedia

  • Type polymorphism — In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions. A function… …   Wikipedia

  • Comparison of Java and C++ — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Polymorphism (computer science) — This article is about the programming language theory concepts with direct application to functional programming languages. For a gentler introduction of these notions as commonly implemented in object oriented programming, see Polymorphism in… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”