Source of Overloading.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program that talks about overloading and demonstrates how we create
  6:  * overloaded methods.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class Overloading {

 12:     private static final Scanner KBD = new Scanner(System.in);

 14:     /**
 15:      * @param args the command line arguments
 16:      */
 17:     public static void main(String[] args) {

 19:         // Introduction
 20:         System.out.println("\n\n"
 21:                 + "We can print many things:");
 22:         System.out.print("\tA String:\t");
 23:         System.out.println("Text");
 24:         System.out.print("\tAn int:  \t");
 25:         System.out.println(10);
 26:         System.out.print("\tA double:\t");
 27:         System.out.println(3.14);
 28:         System.out.print("\tNothing: \t");
 29:         System.out.println();
 30:         pause();

 32:         // Question
 33:         System.out.println("What kind of argument "
 34:                 + "is System.out.println expecting???");
 35:         pause();

 37:         // Explanation
 38:         System.out.println("It's actually four DIFFERENT methods!");
 39:         System.out.println("But all with the same name:");
 40:         System.out.println("\tpublic void println(String s) ...");
 41:         System.out.println("\tpublic void println(int n) ...");
 42:         System.out.println("\tpublic void println(double x) ...");
 43:         System.out.println("\tpublic void println() ...");
 44:         pause();

 46:         // More explanation
 47:         System.out.println("Java knows which one you want "
 48:                 + "by looking at the argument you give it.");
 49:         System.out.println("\tSystem.out.println(\"Text\"); --> "
 50:                 + "public void println(String s) ...");
 51:         System.out.println("\tSystem.out.println(10);     --> "
 52:                 + "public void println(int n) ...");
 53:         System.out.println("\tSystem.out.println(3.14);   --> "
 54:                 + "public void println(double x) ...");
 55:         System.out.println("\tSystem.out.println();       --> "
 56:                 + "public void println() ...");
 57:         pause();

 59:         // Own methods
 60:         doThat();
 61:     }

 63:     /**
 64:      * Pause with a user-defined message. KBD must be empty.
 65:      *
 66:      * @param message   the message to print at the pause
 67:      */
 68:     private static void pause(String message) {
 69:         System.out.print(message);
 70:         KBD.nextLine();
 71:         System.out.println();
 72:     }

 74:     /**
 75:      * Pause with the standard message. KBD must be empty.
 76:      */
 77:     private static void pause() {
 78:         pause("\nPress Enter...");
 79:     }

 81:     // Multiple overloaded versions of doThis.

 83:     // ... one with no arguments
 84:     public static void doThis() {
 85:         System.out.println();
 86:         System.out.println("                       doThis();");
 87:         System.out.println(" => public static void doThis() {...}");
 88:     }

 90:     // ... one with an int
 91:     public static void doThis(int n) {
 92:         System.out.println();
 93:         System.out.println("                       doThis(" + n + ");");
 94:         System.out.println(" => public static void doThis(int n) {...}");
 95:     }

 97:     // ... one with a String
 98:     private static void doThis(String s) {
 99:         System.out.println();
100:         System.out.println("                       doThis(\"" + s + "\");");
101:         System.out.println(" => public static void doThis(String s) {...}");
102:     }

104:     // ... one with a String and an int (in that order)
105:     public static void doThis(String s, int n) {
106:         System.out.println();
107:         System.out.println("                       doThis(\"" + s + "\", " 
108:                 + n + ");");
109:         System.out.println(" => public static void doThis(String s, int n) "
110:                 + "{...}");
111:     }

113:     // ... one with an int and a String (in THAT order)
114:     public static void doThis(int n, String s) {
115:         System.out.println();
116:         System.out.println("                       doThis(" + n 
117:                 + ", \"" + s + "\");");
118:         System.out.println(" => public static void doThis(int n, String s) "
119:                 + "{...}");
120:     }

122:     // ... another one with just an int -- NOT ALLOWED
123:     // ... BECAUSE Java can't tell it from the void doThis(int n) above
124:     // public static int doThis(int m) {
125:     //     System.out.println();
126:     //     System.out.println("                         n = doThis(" + m + ");");
127:     //     System.out.println("        => public static int doThis(int m) {...}");
128:     // }

130:     // public/private, static/not static, return type:  none of them matter!
131:     // All that matters is the name of the method and its parameter list.

133:     /**
134:      * A method to call ALL those overloaded doThis methods.
135:      * Which version of the method does each line call?
136:      */
137:     public static void doThat() {
138:         System.out.println("Here I am calling a LOT of doThis methods!");
139:         doThis();
140:         doThis(5);
141:         doThis("Hello!");
142:         doThis("One", 2);
143:         doThis(3, "Four");
144:         System.out.println();
145:     }
146: }