1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Java Term of the Week: Overloading

Saturday November 8, 2008

I touched on the concept of overloading a little bit last week when talking about method signatures. In case you missed it, a method signature is part of the method declaration and is simply the combination of the method name and parameter list. Overloading is the ability to define more than one method with the same name in a class. When one of the methods is called the compiler uses their method signatures to be able to distinguish between them.

A nice example of overloading is the print method of the System.out object. There are nine different ways the print method can be used:

print.(Object obj)
print.(String s)
print.(boolean b)
print.(char c)
print.(char[] s)
print.(double d)
print.(float f)
print.(int i)
print.(long l)

When you use the print method in your code the compiler will determine which method you want to call by looking at the method signature. For example:

int number = 9;
System.out.print(number);
String text = "nine";
System.out.print(text);
boolean nein = false;
System.out.print(nein);

Each time a different print method is being called because the parameter type being passed is different. It's useful because the print method will need to vary how it works depending on whether it has to deal with a String or integer or boolean.

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.