Overloading Methods Flashcards
Define Method Overloading !
Method overloading in java is using more than one method, with the same name but different parameter list, either in terms of number of paramters or in terms of types of parameters or both, in the same class
Give which parts are relevant and which parts aren’t to overloading !
When it comes to method overloading, the parts that are relevant are the method name and the parameter list, everything else, like the access modifier,the return type, the optional specifiers, the exception list, the method body, are irrelevant to method overloading
Give examples for overloaded methods with all possible scenarios !
All possible scenarios for method overloading are :
- different paramter types
- diffrent number of paramters
- different number of paramters and different types and that’s that
EXAMPLE :
public void fly(int numMiles) { }
public void fly(short numFeet) { }
public boolean fly() { return false; }
void fly(int numMiles, short numFeet) { }
public void fly(short numFeet, int numMiles) throws Exception { }
How does java recognize an overloaded method when it’s being called (give an example and run it) !
Java recognizes an overloaded method when it’s being called because of the type of parameters and the number of parameters being used to call a specific overloaded method.
EXAMPLE :
public static void fly(int numMiles) {
System.out.println(“short”);
}
public static void fly(short numFeet) {
System.out.println(“short”);
}
The call fly((short) 1); prints short. It looks for matching types and calls the appropriate method.
Give the trick used for overloaded methods with varargs (give an example and a counter example and run them) !
The pitfall of using overloaded methods with a vararg parameter is that, java treats a vararg parameter as an actual array which means that 2 methods,
one has an array as a parameter, and the second method has a vararg parameter with same name as the array parameter of the first method, the second method is not considered an overloaded method but it is rather
considered a duplicate of the first method which will result in a
compilation error ( the second method will generate a compilation error)
EXAMPLE :
public void fly(int[] lengths) { }
public void fly(int… lengths) { } // DOES NOT COMPILE
Explain how java handles overloaded methods with regard to autoboxing (give an example and run it) !
When calling a method that has one parameter of type Integer using a primitive int variable, java will convert that variable to the object Integer, but in case we have 2 overloaded methods one has an Integer type parameter and the other has an primitive int parameter , java will always call the method with the more specific type parameter
EXAMPLE :
public void fly(int numMiles) { }
public void fly(Integer numMiles) { }
Java will match the int numMiles version. Java tries to use the most specific parameter list it can find. When the primitive int version isn’t present, it will autobox. However, when the primitive int version is provided, there is no reason for Java to do the extra work of autoboxing.
Give an example for overloaded methods with regard to reference types and run it !
public class ReferenceTypes {
public void fly(String s) {
System.out.print(“string “);
}
public void fly(Object o) {
System.out.print(“object “);
}
public static void main(String[] args) {
ReferenceTypes r = new ReferenceTypes();
r.fly(“test”);
r.fly(56);
} }
The answer is “string object”. The first call is a String and finds a direct match.
There’s no reason to use the Object version when there is a nice String parameter list just
waiting to be called. The second call looks for an int parameter list. When it doesn’t find
one, it autoboxes to Integer to go the Object one.
Give an example for overloaded methods with regard to primitives and run it !
public class Plane {
public void fly(int i) {
System.out.print(“int “);
}
public void fly(long l) {
System.out.print(“long “);
}
public static void main(String[] args) {
Plane p = new Plane();
p.fly(123);
p.fly(123L);
} }
The answer is intlong. The first call passes an int and sees an exact match. The second call passes a long and also sees an exact match.
Give an example of a trick used in overloaded methods with regard to primitives !
public class Plane {
public void fly(long l) {
System.out.print(“long “);
}
public **static void main(String[] args) **{
Plane p = new Plane();
p.fly(123);
p.fly(123L);
} }
the output becomes long long. Java has no problem calling a larger primitive. However, it will not do so unless a better match is not found.
NOTE :
if a method expects a wider type than the argument provided, Java will automatically convert the argument to the wider type.
However, narrowing conversions do not occur automatically; if a method expects a narrower type than the argument provided, you may need to explicitly cast the argument to the narrower type to avoid a compilation error.
Give a concise and accurate general rule when it comes to calling overloaded methods in java !
The rules for when an overloaded method is called should be logical. Java calls the most specific method it can.
When a method is called, Java examines the arguments provided in the method call and compares them to the parameter lists of all available overloaded methods with the same name using the following order:
- Exact match by type
- Larger primitive type
- Autoboxed type
- Varargs
Give one last pitfall when calling an overloaded method (related to autoboxing)
When calling an overloaded method that takes a Long parameter whith an int primitive type, it will result in a compilation error because java can handle
only one conversion , in this case it can not convert form int to long and then from long to Long
EXAMPLE :
public class TooManyConversions {
public static void play(Long l) { }
public static void play(Long… l) { }
public static void main(String[] args) {
play(4); // DOES NOT COMPILE
play(4L); /* calls the Long version */
} }