File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloaded functions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloaded functions?" Watch "Overloaded functions?" New topic
Author

Overloaded functions?

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following:

1. public class MethodOver {
2. private int x, y;
3. private float z;
4. public void setVar(int a, int b, float c){
5. x = a;
6. y = b;
7. z = c;
8. }
9. }

Which two overload the setVar method? (Choose Two)

A. void setVar (int a, int b, float c){
x = a;
310 - 035
y = b;
z = c;
}
B. public void setVar(int a, float c, int b) {
setVar(a, b, c);
}
C. public void setVar(int a, float c, int b) {
this(a, b, c);
}
D. public void setVar(int a, float b){
x = a;
z = b;
}
E. public void setVar(int ax, int by, float cz) {
x = ax;
y = by;
z = cz;
}

Answer: B, D.
My question: Why not C?
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Java Language Specification:
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded.

C will produce compile time error.
[ February 10, 2005: Message edited by: David Ulicny ]

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
In C, u have
public void setVar(int a, float c, int b) {
this(a, b, c);
}

If u see inside the method body u have "this(a,b,c)",
"this " keyword can only be used in a constructor, it cannot be used inside any method, so that would display a compilation error. Thats why i guess its opted out as the right choice
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
But the signatures of options B,C are the same.
So, if B is one of the correct answers, why is not C also correct?
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Because if can't to compile it you can't talk about overloading
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Thanks Animesh and David, I've got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloaded functions?