• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overload

 
Sangita Mishra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is probably 1,3,5 or 1,4,5

Shashank
 
sabeer
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
narendra darlanka
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic