aspose file tools*
The moose likes Mock Exam Errata and the fly likes ques from overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "ques from overloading" Watch "ques from overloading" New topic
Author

ques from overloading

gaurav nayyar
Greenhorn

Joined: Jul 13, 2001
Posts: 9
What will happen if you try to compile and run this code.
class Rectangle{
public int area(int length , int width) {
return length * width;
}
}
class Square extends Rectangle{
public int area(long length , long width) {
return (int) Math.pow(length ,2);
}
}
class Test{
public static void main(String args[]) {
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
1. Will not compile.
2. Will compile and run printing out 20
3. Runtime error
4. Will compile and run printing out 25
the right answer is :1 how??? i can't get it
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi gaurav,
The methods area(int,int) and area(long, long) are ambiguous i.e. there is no way for the compiler to differentiate between the two as many values will satisfy both 'int' and 'long' parameters.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39

the problem is "r.area(5,4)" method call, the compiler will look for the four possible implmentation of this method.
area( int, int );
area( int, long );
area( long, int );
area( long, long );
in this case, the compiler found two implementations available, and it got cofused since it doesn't know which implementation you really want to invoke.
Wei
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
But i thought this would be a question of overloading. The compiler would take the most specific option
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
It does involve overloading. The problem is overloading is resolved at compile-time and in this example the arguments being passed '5' and '4' can be used as both int's and long's so the compiler isnt' sure which overloaded method it should use.
Remember, the code is compiled to bytecode and this is the problem, should the compiler create the bytecode for <code>area(int,int)</code> or <code>area(long, long)</code>? If you change the call to <code>r.area( 5L, 4L )</code>, the code will compile. As it stands, the compiler can't tell what your intention is. If you only want to use 'int' types, remove the 'long' method. If you want to use both, remove the 'int' method.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
If the two methods are in the same class then no error occurs. Why?

Thanks,
Vanitha.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Vanitha,
I think (but I'm not 100% sure) in the first example part of the problem is the compiler doesn't know wether to use the method in the Square class or the parent Rectangle class ie you've got an object 'r' which is both a Square and a Rectangle. Should the superclass behaviour be used or the subclass behaviour? The compiler doesn't know for sure.
When both methods are in the same class it doesn't really matter which is used; the object is a Square but in this case it has no behviours that are different from the superclass so it can be handled purely as a Rectangle ie there's no conflict between the class behaviours.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your explanation Jane, It is clear now.
Vanitha.
gaurav nayyar
Greenhorn

Joined: Jul 13, 2001
Posts: 9
thanks to all of u now its clear
gaurav nayyar
Greenhorn

Joined: Jul 13, 2001
Posts: 9
thanks to all of u now its clear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ques from overloading