Creating Methods with the Same Name, Different Arguments

When you work with Java's class library, you often encounter classes that have numerous methods with the same name.

Methods with the same name are differentiated from each other by two things:

  • The number of arguments they take

  • The data type or objects of each argument

These two things are part of a method's signature, and using several methods with the same name and different signatures is called overloading.

Method overloading can eliminate the need for entirely different methods that do essentially the same thing. Overloading also makes it possible for methods to behave differently based on the arguments they receive.

When you call a method in an object, Java matches the method name and ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.