Overloading

In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different.

// Demonstrate method overloading.
class OverloadDemo {
 void test() {
 System.out.println("No parameters");
 }

// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}

// Overload test for two integer parameters.
 void test(int a, int b) {
 System.out.println("a and b: " + a + " " + b);
 }

// overload test for a double parameter
 double test(double a) {
 System.out.println("double a: " + a);
 return a * a;
 }
}

class Overload {
 public static void main(String args[]) {
 OverloadDemo ob = new OverloadDemo();
 double result;
 // call all versions of test()
 ob.test();
 ob.test(10);
 ob.test(10, 20);
 result = ob.test(123.25);
 System.out.println("Result of ob.test(123.25): " + result);
 }
}

This program generates the following output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

Automatic type conversions apply to overloading.

class OverloadDemo {
 void test() {
 System.out.println("No parameters");
 }

// Overload test for two integer parameters.
 void test(int a, int b) {
 System.out.println("a and b: " + a + " " + b);
 }

// overload test for a double parameter
 void test(double a) {
 System.out.println("Inside test(double) a: " + a);
 }
}

class Overload {
 public static void main(String args[]) {
 OverloadDemo ob = new OverloadDemo();
 int i = 88;
 ob.test();
 ob.test(10, 20);
 ob.test(i); // this will invoke test(double)
 ob.test(123.2); // this will invoke test(double)
 }
}

This program generates the following output:
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2

Overloading Constructors

class Box {
 double width;
 double height;
 double depth;

// constructor used when all dimensions specified
 Box(double w, double h, double d) {
 width = w;
 height = h;
 depth = d;
 }

// constructor used when no dimensions specified
 Box() {
 width = -1; // use -1 to indicate
 height = -1; // an uninitialized
 depth = -1; // box
 }

// constructor used when cube is created
 Box(double len) {
 width = height = depth = len;
 }

// compute and return volume
 double volume() {
 return width * height * depth;
 }
}

class OverloadCons {
 public static void main(String args[]) {
 // create boxes using the various constructors
 Box mybox1 = new Box(10, 20, 15);
 Box mybox2 = new Box();
 Box mycube = new Box(7);
 double vol;
 // get volume of first box
 vol = mybox1.volume();
 System.out.println("Volume of mybox1 is " + vol);
 // get volume of second box
 vol = mybox2.volume();
 System.out.println("Volume of mybox2 is " + vol);
 // get volume of cube
 vol = mycube.volume();
 System.out.println("Volume of mycube is " + vol);
 }
}

This program generates the following output:
No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2

 

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑