• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ques from overloading

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But i thought this would be a question of overloading. The compiler would take the most specific option
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If the two methods are in the same class then no error occurs. Why?

Thanks,
Vanitha.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your explanation Jane, It is clear now.
Vanitha.
 
gaurav nayyar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all of u now its clear
 
gaurav nayyar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all of u now its clear
reply
    Bookmark Topic Watch Topic
  • New Topic