Home : Course Map : Chapter 3 : Java :
Method Overloading
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Suppose that you create a class that includes a method with an integer argument:

   void A_method (int k) {...}

You decide later that you need a method that accomplishes the same task but it requires a float argument. In some procedural languages, you would create a new method with a slightly different name:

   void A_method_f (float x) {...}

A more elegant solution would allow you to use exactly the same name for the new method and the compiler would determine by the argument type which method to use.

   void A_method (int k) {...}

   void A_method (float x) {...}

In OOP this is allowed (and encouraged) and is called overloading. For example, we have used the static method

   System.out.println (String)

to print messages to the Java console. This is actually just one of several println(...) methods in the PrintStream class, an instance of which the static variable System.out references. The other overloaded versions of println() include:

println () <- prints out a line separator
println (boolean)
println (char)
println (char[])
println (double)
println (float)
println (int)
println (long)
println (java.lang.Object)

Overloaded methods with the same argument list must all have the same return type. Otherwise, either the number of arguments or the type of arguments must differ among the overloaded methods.

We saw in the previously the example of overloading of constructors. This allows the option of creating a class with alternative initializations. The discussion of this(), super() in Chapter 4 will return to the case of overloaded constructors.

Latest update: Oct. 16, 2004

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.