• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloaded method

 
Shweta R Dhaneshwar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloaded method area has 2 versions in the class hierarchy.What I know about overloading is that during compilation of the source code, depending upon the type of arguments passed and the type of reference,appropriate method is called.I have also read somewhere that when more than one method matches method call signature,the most specific implementation of the method is called.Is it true in case of overloaded as well as overriding methods...am I confusing between two different things?Why is it that we are getting this error?According to me method area(int,int) from class Rectangle should be called.


o/p:

D:\Java\EditPlus 2>javac Test.java
Test.java:15: reference to area is ambiguous, both method area(int,int) in Recta
ngle and method area(long,long) in Square match
System.out.println(r.area(5 , 4));
^
1 error
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m using java 1.4.2
and its running fine


see the screen output

D:\java_prac>javac Test06.java

D:\java_prac>java Test06
20


check ur version..
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compiler are you using? I just compiled your code and ran it with no problem. Here's the result of "java -version" on my pc:

java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

By the way, the result was what you would expect: the superclass version of the method executed.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Java 2 SDK, Version 1.4.2

The compiler tells me
<B>The method area(long, long) is ambiguous for the type Square</B>

The declared type of <B>r</B> is Square, therefore the method <B>area</B> from both Square as well as its superclass Rectangle are applicable.

Now since the arguments to the method area are integer literals, the more specific of the two methods is applicable.

Here the more specific of two is that of the Square class that is area(int, int) is more applicable, as 5 and 4 both are integer literals.

We know that a method invocation can widen an argument of type int to match a method parameter of type long so any argument that can be passed to area(int, int) can also be passed to area(long, long) without generating a compile-time error. Since both methods are applicable, the more specific of the two, area(int,int) is chosen over the less specific area(long,long).

Philip could you please explain the statement quoted below in context to what I said above, and correct me if I am wrong.
By the way, the result was what you would expect: the superclass version of the method executed.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled and exceuted the program on Java version 1.5, the java -version told me

java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b64)
Java HotSpot(TM) Client VM (build 1.5.0_02-b64, mixed mode, sharing)

It compiled fine and gave me an output of 20 which is the area method of the Rectangle class.

Is there problem with the versions or what, but again Amit did compile it with Java 1.4.2.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya it compiled and on java -version command it prints


D:\java_prac>java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

................
anyway ..i just wana ask that all program which run on 1.4.2 will run on jdk 1.5 ??? in addition to what jdk 1.5 feature brings ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic