Declaring Overloaded Methods
Class MethodOverload
(Fig. D.7) includes two overloaded versions of method square
—one that calculates the square of an int
(and returns an int
) and one that calculates the square of a double
(and returns a double
). Although these methods have the same name and similar parameter lists and bodies, think of them simply as different methods. It may help to think of the method names as “square
of int
” and “square
of double
,” respectively.
1 // Fig. D.7: MethodOverload.java 2 // Overloaded method declarations. 3 4 public class MethodOverload 5 { 6 // test overloaded square methods 7 public static void main( String[] args ) 8 { 9 System.out.printf( "Square of ...
Get Android™ How to Program, 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.