Advanced Methods

Vocab Bar
Method Call A statement that calls a method, which runs the method with the parameters given.
Method Overloading A feature of Java that allows more than one methods to have the same name but different parameter lists.

You have now learned the fundamental structure of methods, but there are still many other features you have to learn. For example, take the code below that was shown in lesson 1 (What is Programming).

	public static String toBaseN(int num, int base) {

		String newNum = "";

		while (num > 0) {

			int result = num % base;

			newNum = result + newNum;

			num /= base;

		}

		return newNum;

	}

This method converts a number to a different base. In this lesson, we cover how are we supposed to use this method. In order to make this method run, we have to add a method call somewhere else in the program. Here is the structure of a method call:

Below, there is a main method with a bolded method call to the method toBaseN(int num, int base).


	public static void main(String[] args) {

		String newValue = toBaseN(55, 2);

		System.out.println(newValue); // will output 110111

	}

The method name signals the computer as to which method is being used, and the parameters list provides the specific inputs needed for that method. If the method has no parameters, the parameter list is left empty. For example, if the method toBaseN(int num, int base) had no parameters, the method call would be:

	toBaseN();

Note: The method calls toBaseN(55, 2) and toBaseN() only work if it is being called in the same class. Since classes have not been covered, and we have only worked in one class so far, simply understand this syntax only works when working in a single class.

Finally, method overloading is used when you want to methods to have the same name and they have different parameter lists. Below is an example of two methods that demonstrate this idea:

	// adds the word two to the end of a string

	public String addTwo(String str) {

		return (str + " two");

	}

	// adds the number two to an integer

	public int addTwo(int num) {

		return (num + 2);

	}

Both of these methods may be useful, and both are appropriately named addTwo, so method overloading is brought into effect. Now, it is useful to note that method overloading is caused by different parameters, not by different return types. Method overloading still works when the methods have the same return types, but does not work when they have the same parameters. Below is an example of an invalid overloading method:

	// adds the word two to the end of a string

	public String addTwo(String str) {

		return (str + " two");

	}

	// adds the number two to the end of a string

	public int addTwo(String num) {

		return (num + 2);

	}

A method call of addTwo in the above scenario would result in an error because they have the same parameter type (String). Method calls and the concept of method overloading are both important to understanding how to create and use methods.

Lesson Quiz

1. What is method overloading?

a. An error produced when you have to many methods.
b. Changing the output of one method in another method.
c. Two or more methods having the same name with different parameter lists.
d. The act of calling a method.

2. What is wrong with the code below?

	public static int addTwo(int num) {

		return (num + 2);

	}

	public static void main(String[] args) {

		System.out.println(addTwo());

	}
a. The method addTwo does not return an integer.
b. The method call of addTwo does not have a complete parameter list.
c. The main method is incorrectly formatted.
d. The method name addTwo does not match its purpose.

Written by Chris Elliott

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.