Lesson: Overloading

Overview: 
Explore what "overloading" is and how it is used with methods.
Objectives: 

Understand the "overloading" concept and how it applies to methods and how to use it.

Content: 

Remember that we mentioned that a method's name and parameter list are called the method's signature and this signature is how Java identifies methods. So it follows that while methods with a different name are definitely not the same method, methods with the same name and different parameter lists are also not the same method. This allows you to use different parameter lists to create variations of a method. This is called overloading. Here is an example of overloading:

The calling code can call printout() with a String or an int and both calls will work. Here is an example showing varying the number of parameters:

Calling code can use either method but the second has the option to return the area in meters. Note that the second method reuses the first method so that the basic area computation is only defined in one place. In effect, the second method "adds" functionality to the first.

Here is this example in CodingGround.

 

Navigation: