Java Term of the Week: Overloading
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.


No comments yet. Leave a Comment