Witscale Test Center

11.3 Method overloading and overriding > 11.3.1 Method overloading


11.3.1 Method overloading

Java methods are uniquely identified by the method signature. A method signature consists of method name, its argument types in the exact sequence and the return type. Java considers two methods different from each other if their signatures are different. Therefore, Java can distinguish between two methods if their name is same but their arguments are different. For instance if you write following methods in one class, they will be treated as separate methods:

 

public void someMethod(int i, float f) {}

public void someMethod(int i, int j) {}

public void someMethod(float f) {}

public boolean someMethod() {}

 

All the four methods have same method name but their signatures are different. Thus, you can reuse the method name someMethod to define four different methods within the same class. Such methods are called as the overloaded methods. All the four methods are unique within one class because each of them has unique signature. You can find examples of overloaded method in Java API packages. The valueOf methods of String class or the print methods of java.io.PrintStream class are overloaded.

 

Another type of overloading is the operator overloading. The arithmetic operators such as + - * / can be used to add integers as well as floating-point numbers.  This is possible because these symbols are overloaded to work on multiple types of arguments. Java has the operators overloaded within the language. You cannot programmatically overload operator in Java.

 

Things to remember while overloading method-name

Since only the method name is reused in overloading, the method overloading is actually method name overloading. You should overload method names only when you are sure that the methods are performing same basic task on different data sets. Methods with unrelated functionality should have different names. In addition, following are few things you must remember while overloading methods.

 

Each of the overloaded methods must have a unique signature.

We saw earlier how the overloaded methods are distinguished based on their signature. Note that Java compiler cannot differentiate the methods based on only the return type. For example, if you write following two methods in one class, the compiler will flag an error saying, “Duplicate method someMethod...

 

public boolean someMethod() {return false;}

public int someMethod() { return 0;}        // error!

 

You may think that the above two methods are distinct, but for Java compiler they are same because their name and argument list is same. Hence they cannot be appear in one class.

 

Exam may have questions where you will be presented with method definitions with the same method name and you will be asked whether they both can be in the same class. While answering this type of questions, check the signatures of methods carefully. If they are same, the method declarations are not valid.

 

Overloaded methods may include the inherited methods of a class

Since the only criterion for overloaded methods is unique signature with same method name, the inherited methods can be also be part of the overloaded methods. In the following example, the class FileLogger is a subclass of class Logger.

 

public class Logger {

   public void log(String strObj)  {}

}

 

public class FileLogger extends Logger {

   public void log(Date dateObj)  {}

}

 

FileLogger will inherit log(String) method of Logger. Therefore it will have two overloaded methods; the one it inherits (public void log(String strObj)) and the other it defines (public void log(Date dateObj)).

 

Overloaded methods are distinct methods of a class therefore…

1.       They can call each other just like any other method.

For example, if a class Logger has overloaded log methods, they can call each other just the way they call other class methods. In the following example, only the method log(String strObj) is actually implementing the logging functionality. The rest of the log methods converts their argument in string and call this overloaded method.

 

public void log(String strObj)  {

 // actually logging

}

 

public void log(int i)  {

  String stringValue = Integer.toString(i, 10);

  log(stringValue);

}

 

public void log(boolean b)  {

  String stringValue = b? "true":"false";

  log(stringValue);

}

 

Often the overloaded methods perform related functionality on different types of arguments. Therefore, it is a common practice to implement functionality in one of the overloaded method, for example, log(String strObj) and the rest of overloaded methods will only do necessary type change and call the log method  implementing the actual logging.

 

2.       They can have different access modifiers and can declare exceptions independent of each other.

Following methods are all valid overloaded methods.

 

public void log(String str) throws FileNotFoundException {}

void log(int i) {}

private void log() {}

 

Note that methods are not considered distinct if only their access modifiers are different. Therefore following two methods cannot be declared within same class.

 

private void log(int i) {}

void log(int j) {} // Error!

 

The bottom line is that two methods with same name are considered as overloaded (within a class) only when the combination of their arguments types is different.