Home : Course Map : Chapter 3 : Java :
Demos 5: 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

Here we give an example of method overloading. We use the starters StartApp2.java and StartApplet2.java, which include the class Test for inserting methods and data fields. In the main routine, which is where the processing begins, we create an instance of the Test class and invoke its methods.

OverloadApplet.java
(Output goes to browser's Java console.)

public class OverloadApplet extends java.applet.Applet
{
  public void init () {
    //Create an instance of the Test class
     Test test = new Test ();

     // Invoke all three overloaded versions of aMethod
     // in the class Test.
     test.aMethod (3);
     test.aMethod (4,5.6);
     test.aMethod (7,8.9,true);
  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g) {
     g.drawString ("OverloadApplet", 20, 20);
  }
}

// The Test class holds three overloaded versions of aMethod().
class Test
{
  int i = 0;
  double x = 0.0;
  boolean flag = false;

  void aMethod (int j)  {
    i = j;
    System.out.println ("In aMethod (int j)");
    System.out.println ("i = " + i);
    System.out.println ("x = " + x);
    System.out.println ("flag = " + flag);
    System.out.println ();
  }

  void aMethod (int j, double y) {
    i = j;
    x = y;
    System.out.println ("In aMethod (int j, double y)");
    System.out.println ("i = " + i);
    System.out.println ("x = " + x);
    System.out.println ("flag = " + flag);
    System.out.println ();
  }

  void aMethod (int j, double y, boolean b) {
    i = j;
    x = y;
    flag = b;
    System.out.println ("In aMethod (int j, double y, boolean b)");
    System.out.println ("i = " + i);
    System.out.println ("x = " + x);
    System.out.println ("flag = " + flag);
    System.out.println ();
  }
}

OverloadApp.java - application version


Latest update: Oct.19, 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.