Declaring Overloaded Methods

Class MethodOverload (Fig. 6.10) 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. 6.10: 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 Java™ How To Program (Early Objects), Tenth 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.