Java Methods and Classes

A Closer Look at Methods and Classes

Overloading Methods

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. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. Method overloading is one of Java’s most useful features. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus, overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Here is a simple example that illustrates method overloading:

 

// 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

 

As you can see, test( ) is overloaded four times. The first version takes no parameters, the second takes one integer parameter, the third takes two integer parameters, and the fourth takes one double parameter. The fact that the fourth version of test( ) also returns a value is of no consequence relative to overloading, since return types do not play a role in overload resolution. When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method’s parameters. However, this match need not always be exact. For example, consider the following program:

 

// 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

 

As you can see, this version of OverloadDemo does not define test(int). Therefore, when test( ) is called with an integer argument inside Overload, no matching method is found. However, Java can automatically convert an integer into a double, and this conversion can be used to resolve the call. Therefore, after test(int) is not found, Java elevates i to double and then calls test(double). Of course, if test(int) had been defined, it would have been called instead. Java will employ its automatic type conversions only if no exact match is found. Method overloading supports polymorphism because it is one way that Java implements the “one interface, multiple methods” model. To understand how, consider the following. In languages that do not support method overloading, each method must be given a unique name. However, frequently you will want to implement essentially the same method for different types of data.

Overloading Constructors

In addition to overloading normal methods, you can also overload constructor methods. To understand why, let’s return to the Box class developed in the preceding chapter. Following is the latest version of Box:

class Box {

double width;

double height;

double depth;

// This is the constructor for Box.

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

// compute and return volume

double volume() {

return width * height * depth;

}

}

 

As you can see, the Box( ) constructor requires three parameters. This means that all declarations of Box objects must pass three arguments to the Box( ) constructor. For example, the following statement is currently invalid:

 

Box ob = new Box();

 

Since Box( ) requires three arguments, it’s an error to call it without them. Here is a program that contains an improved version of Box that does just that:

 

/* Here, Box defines three constructors to initialize

the dimensions of a box various ways.

*/

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);

}

}

 

The output produced by this program is shown here:

Volume of mybox1 is 3000.0

Volume of mybox2 is -1.0

Volume of mycube is 343.0

 

As you can see, the proper overloaded constructor is called based upon the parameters specified when new is executed.

Using Objects as Parameters

 

So far we have only been using simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For example, consider the following short program:

 

// Objects may be passed to methods.

class Test {

int a, b;

Test(int i, int j) {

a = i;

b = j;

}

// return true if o is equal to the invoking object

boolean equals(Test o) {

if(o.a == a && o.b == b) return true;

else return false;

}

}

class PassOb {

public static void main(String args[]) {

Test ob1 = new Test(100, 22);

Test ob2 = new Test(100, 22);

Test ob3 = new Test(-1, -1);

System.out.println(“ob1 == ob2: ” + ob1.equals(ob2));

System.out.println(“ob1 == ob3: ” + ob1.equals(ob3));

}

}

 

This program generates the following output:

ob1 == ob2: true

ob1 == ob3: false

 

As you can see, the equals( ) method inside Test compares two objects for equality and returns the result. That is, it compares the invoking object with the one that it is passed. If they contain the same values, then the method returns true. Otherwise, it returns false. Notice that the parameter o in equals( ) specifies Test as its type. Although Test is a class type created by the program, it is used in just the same way as Java’s built-in types. One of the most common uses of object parameters involves constructors. Frequently you will want to construct a new object so that it is initially the same as some existing object. To do this, you must define a constructor that takes an object of its class as a parameter. For example, the following version of Box allows one object to initialize another:

 

 

// Here, Box allows one object to initialize another.

class Box {

double width;

double height;

double depth;

// construct clone of an object

Box(Box ob) { // pass object to constructor

width = ob.width;

height = ob.height;

depth = ob.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 OverloadCons2 {

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);

Box myclone = new Box(mybox1);

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 cube is ” + vol);

// get volume of clone

vol = myclone.volume();

System.out.println(“Volume of clone is ” + vol);

}

}

Argument Passing

In general, there are two ways that a computer language can pass an argument to a subroutine. The first way is call-by-value. This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an argument can be passed is call-by-reference. In this method, a reference to an argument not the value of the argument is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine. As you will see, Java uses both approaches, depending upon what is passed. In Java, when you pass a simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method. For example, consider the following program:

 

// Simple types are passed by value.

class Test {

void meth(int i, int j) {

i *= 2;

j /= 2;

}

}

class CallByValue {

public static void main(String args[]) {

Test ob = new Test();

int a = 15, b = 20;

System.out.println(“a and b before call: ” +

a + ” ” + b);

ob.meth(a, b);

System.out.println(“a and b after call: ” +

a + ” ” + b);

}

}

 

The output from this program is shown here:

a and b before call: 15 20

a and b after call: 15 20

 

As you can see, the operations that occur inside meth( ) have no effect on the values of a and b used in the call; their values here did not change to 30 and 10. When you pass an object to a method, the situation changes dramatically, because objects are passed by reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by-reference. Changes to the object inside the method do affect the object used as an argument. For example, consider the following program:

 

// Objects are passed by reference.

class Test {

int a, b;

Test(int i, int j) {

a = i;

b = j;

}

// pass an object

void meth(Test o) {

o.a *= 2;

o.b /= 2;

}

}

class CallByRef {

public static void main(String args[]) {

Test ob = new Test(15, 20);

System.out.println(“ob.a and ob.b before call: ” +

ob.a + ” ” + ob.b);

ob.meth(ob);

System.out.println(“ob.a and ob.b after call: ” +

ob.a + ” ” + ob.b);

}

}

 

This program generates the following output:

ob.a and ob.b before call: 15 20

ob.a and ob.b after call: 30 10

 

As you can see, in this case, the actions inside meth( ) have affected the object used as an argument. As a point of interest, when an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.

Returning Objects

A method can return any type of data, including class types that you create. For example, in the following program, the incrByTen( ) method returns an object in which the value of a is ten greater than it is in the invoking object.

 

// Returning an object.

class Test {

int a;

Test(int i) {

a = i;

}

Test incrByTen() {

Test temp = new Test(a+10);

return temp;

}

}

class RetOb {

public static void main(String args[]) {

Test ob1 = new Test(2);

Test ob2;

ob2 = ob1.incrByTen();

System.out.println(“ob1.a: ” + ob1.a);

System.out.println(“ob2.a: ” + ob2.a);

ob2 = ob2.incrByTen();

System.out.println(“ob2.a after second increase: ”

+ ob2.a);

}

}

 

The output generated by this program is shown here:

ob1.a: 2

ob2.a: 12

ob2.a after second increase: 22

 

As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference to it is returned to the calling routine. Since all objects are dynamically allocated using new, you don’t need to worry about an object going  out-of-scope because the method in which it was created terminates. The object will continue to exist as long as there is a reference to it somewhere in your program. When there are no references to it, the object will be reclaimed the next time garbage collection takes place.

 

Recursion

Java supports recursion. Recursion is the process of defining something in terms of itself. As it relates to Java programming, recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive. The classic example of recursion is the computation of the factorial of a number. The factorial of a number N is the product of all the whole numbers between 1 and N. For example, 3 factorial is 1 * 2 * 3, or 6. Here is how a factorial can be computed by use of a recursive method:

 

// A simple example of recursion.

class Factorial {

// this is a recursive function

int fact(int n) {

int result;

if(n==1) return 1;

result = fact(n-1) * n;

return result;

}

}

class Recursion {

public static void main(String args[]) {

Factorial f = new Factorial();

System.out.println(“Factorial of 3 is ” + f.fact(3));

System.out.println(“Factorial of 4 is ” + f.fact(4));

System.out.println(“Factorial of 5 is ” + f.fact(5));

}

}

 

The output from this program is shown here:

Factorial of 3 is 6

Factorial of 4 is 24

Factorial of 5 is 120

 

When fact( ) is called with an argument of 1, the function returns 1; otherwise it returns the product of fact(n–1)*n. To evaluate this expression, fact( ) is called with n–1. This process repeats until n equals 1 and the calls to the method begin returning.  When you compute the factorial of 3, the first call to fact( ) will cause a second call to be made with an argument of 2. This invocation will cause fact( ) to be called a third time with an argument of 1. This call will return 1, which is then multiplied by 2 the value of n in the second invocation. This result which is 2 is then returned to the original invocation of fact( ) and multiplied by 3 the original value of n. This yields the answer, 6. Here is one more example of recursion. The recursive method printArray( ) prints the first i elements in the array values.

 

// Another example that uses recursion.

class RecTest {

int values[];

RecTest(int i) {

values = new int[i];

}

// display array — recursively

void printArray(int i) {

if(i==0) return;

else printArray(i-1);

System.out.println(“[” + (i-1) + “] ” + values[i-1]);

}

}

class Recursion2 {

public static void main(String args[]) {

RecTest ob = new RecTest(10);

int i;

for(i=0; i<10; i++) ob.values[i] = i;

ob.printArray(10);

}

}

This program generates the following output:

[0] 0

[1] 1

[2] 2

[3] 3

[4] 4

[5] 5

[6] 6

[7] 7

[8] 8

[9] 9

Introducing Access Control

 

encapsulation links data with the code that manipulates it. encapsulation provides another important attribute: access control. Through encapsulation, you can control what parts of a program can access the members of a class. By controlling access, you can prevent misuse. For example, allowing access to data only through a well-defined set of methods, you can prevent the misuse of that data. How a member can be accessed is determined by the access specifier that modifies its declaration. Java supplies a rich set of access specifiers. A package is, essentially, a grouping of classes. Java’s access specifiers are public, private, and protected. Java also defines a default access level. protected applies only when inheritance is involved. When a member of a class is modified by the public specifier, then that member can be accessed by any other code. When a member of a class is specified as private, then that member can only be accessed by other members of its class. That is why  main( ) has always been preceded by the public specifier. It is called by code that is outside the program that is, by the Java run-time system. When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package. In the classes developed so far, all members of a class have used the default access mode, which is essentially public.  An access specifier precedes the rest of a member’s type specification. That is, it must begin a member’s declaration statement. Here is an example:

 

public int i;

private double j;

private int myMethod(int a, char b) { // …

 

To understand the effects of public and private access, consider the following program:

 

/* This program demonstrates the difference between

public and private.

*/

class Test {

int a; // default access

public int b; // public access

private int c; // private access

// methods to access c

void setc(int i) { // set c’s value

c = i;

}

int getc() { // get c’s value

return c;

}

}

class AccessTest {

public static void main(String args[]) {

Test ob = new Test();

// These are OK, a and b may be accessed directly

ob.a = 10;

ob.b = 20;

// This is not OK and will cause an error

// ob.c = 100; // Error!

// You must access c through its methods

ob.setc(100); // OK

System.out.println(“a, b, and c: ” + ob.a + ” ” +

ob.b + ” ” + ob.getc());

}

}

 

As you can see, inside the Test class, a uses default access, which for this example is the same as specifying public. b is explicitly specified as public. Member c is given private access. This means that it cannot be accessed by code outside of its class. So, inside the AccessTest class, c cannot be used directly. It must be accessed through its public methods: setc( ) and getc( ). If you were to remove the comment symbol from the beginning of the following line,

 

// ob.c = 100; // Error!

 

Then you would not be able to compile this program because of the access violation.

static

There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist. Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable. Methods declared as static have several restrictions:

  • They can only call other static methods.
  • They must only access static data.
  • They cannot refer to this or super.

 

If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:

 

// Demonstrate static variables, methods, and blocks.

class UseStatic {

static int a = 3;

static int b;

static void meth(int x) {

System.out.println(“x = ” + x);

System.out.println(“a = ” + a);

System.out.println(“b = ” + b);

}

static {

System.out.println(“Static block initialized.”);

b = a * 4;

}

public static void main(String args[]) {

meth(42);

}

}

 

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

 

Here is the output of the program:

Static block initialized.

x = 42

a = 3

b = 12

 

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:

 

classname.method( )

 

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object- reference variables. A static variable can be accessed in the same way by use of the dot operator on the name of the class. Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.

 

class StaticDemo {

static int a = 42;

static int b = 99;

static void callme() {

System.out.println(“a = ” + a);

}

}

class StaticByName {

public static void main(String args[]) {

StaticDemo.callme();

System.out.println(“b = ” + StaticDemo.b);

}

}

 

Here is the output of this program:

a = 42

b = 99

Introducing final

A variable can be declared as final. Doing so prevents its contents from being modified. This means that you must initialize a final variable when it is declared. For example:

 

final int FILE_NEW = 1;

final int FILE_OPEN = 2;

final int FILE_SAVE = 3;

final int FILE_SAVEAS = 4;

final int FILE_QUIT = 5;

 

Subsequent parts of your program can now use FILE_OPEN, etc., as if they were constants, without fear that a value has been changed. It is a common coding convention to choose all uppercase identifiers for final variables. Variables declared as final do not occupy memory on a per-instance basis. Thus, a final variable is essentially a constant.

Using Command-Line Arguments

 

Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ). A command-line argument is the information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy they are stored as strings in the String array passed to main( ). For example, the following program displays all of the command-line arguments that it is called with:

 

// Display all command-line arguments.

class CommandLine {

public static void main(String args[]) {

for(int i=0; i<args.length; i++)

System.out.println(“args[” + i + “]: ” +

args[i]);

}

}

 

Try executing this program, as shown here:

java CommandLine this is a test 100 -1

 

When you do, you will see the following output:

args[0]: this

args[1]: is

args[2]: a

args[3]: test

args[4]: 100

args[5]: -1