This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overload" Watch "Overload" New topic
Author

Overload

Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
What would be answer?

Consider the following method...
public int setVar(int a, int b, float c) { ...}
Which of the following methods correctly overload the above method ?

1. public int setVar(int a, float b, int c)
{
return setVar(a, c, b);
}
2. public int setVar(int a, float b, int c)
{
return this(a, c, b);
}
3. public int setVar(int x, int y, float z)
{
return x+y;
}
4. public float setVar(int a, int b, float c)
{
return c*a;
}
5. public float setVar(int a)
{
return a;
}
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
Truly, the best way to test which method signatures overload the original method signature is to try to compile the code in a test class.
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
the following methods are correctly overloaded
1] , 2] , 5]

the methods that will cause conflicts are 3] and 4] , since
a] the number of formal parameters they all have is the same
b] the name of the method is the same
c] the type of the formal parameters is the same
because of this the compiler will not be able to resolve a method call as a result of which it will throw a compile time error

fellow ranch memebers do comment on the response

regards ,
bhavesh
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
The answer is probably 1,3,5 or 1,4,5

Shashank


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
Overloading means Having two functions with the same name in the same method, but that differ in the types of their arguments. It is not legal to have two methods of the same name that differ only in their return types.
SO, the options 1,2,5 are satisfy the above rule.
Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
The answer given is 1 and 5. 2 can't be the answer as this can only be called from a constructor.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
I don't get the meanin of...

"2 can't be the answer as this can only be called from a constructor."

... is anybody here???


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
narendra darlanka
Ranch Hand

Joined: Jun 17, 2005
Posts: 66
yes 2 cannot be an answer because it uses an call to another constructor in the same class by using the statement "this(arguments..)" and an constructor cannot be called from an method.


class abc
{
int a=0;
abc()
{
this(int 3); //1 legal
}

abc(int x) //2
{
a=x;
}
public void method()
{
this(3); //illegal
}


/...
/....

}

//1 calls the constructor abc(int) from abc() such an statement can be used only in an constructor

~naren


~naren<br /> scjp1.4<br /> scwcd1.4
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
oh well, now I get the meaning of "this" in this stamement

"this can only be called from a constructor"

Actually it should have been,
"this() can only be called from a constructor"

Well, I hate this.

where "this" = name of the keyword. I wish if gosling had replaced this with something else. Thans naren!
[ June 30, 2005: Message edited by: Akhil Trivedi ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Truly, the best way to test which method signatures overload the original method signature is to try to compile the code in a test class.

You'd think so, but because the specification has changed three times since the first GA of 1.5.0 with respect to overloaded method resolution, I'd question that technique.
The issue affected the JTiger test framework: http://forum.jtiger.org/posts/list/24.page


Tony Morris
Java Q&A (FAQ, Trivia)
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"You'd think so, but...."

To my eyes the output and whether the compilation succeeds should be the same under both JDK 1.4.x and 1.5.x. Nonetheless, your comment contains a valid consideration: a candidate for 310-027 should test against JDK 1.4.x, and a candidate for 310-055 should test against the latest JDK 1.5.

My point is that questions that can be answered by the Java compiler and JRE are best answered by the Java Compiler and JRE. This question is essentially a "will this compile?" or "what is the output?" question. I've found that the compiler often catches code that my scrupulous programming and meticulous review miss. Go figure. :roll:
Ramesh Mangam
Greenhorn

Joined: May 03, 2005
Posts: 13

The answer given is 1 and 5. 2 can't be the answer as this can only be called from a constructor.


You could also think of a different reason: Constructors do not have a return type. They are not methods. So, they cannot be used to return 'to' anything.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

a candidate for 310-027 should test against JDK 1.4.x, and a candidate for 310-055 should test against the latest JDK 1.5.

You're right for this case that the output is the same - I was referring to method overload resolution in general, which changed between update versions, and also between beta releases.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

But we can call one constructor from another constructor.
since constructor dint have return type even void .
except that very thing is overloading.
if my perception is wrong ....pls clear ...



Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
 
jQuery in Action, 2nd edition
 
subject: Overload