6.4 Overloading
A good name is better than precious ointment …
—ECCLESIASTES 7:1
You have seen that two or more different classes can define methods having the same name. For example, many classes have a method named readInput
. This is not so surprising. The type of the calling object allows Java to decide which definition of the method readInput
to use. You may be more surprised to hear that you can also have two or more methods in the same class that have the same method name. How is this possible? Read on.
Overloading Basics
When you give two or more methods the same name within the same class, you are overloading the method name. To do this, you must ensure that the different method definitions have something different about their parameter ...
Get Java: An Introduction to Problem Solving and Programming, 8th 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.