• 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

Specific match rule

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the the following code sample:

class Test6 {
void met1(long x, long y) { System.out.println("long"); }
void met2(int x, int y) { System.out.println("int"); }

public static void main(String[] args) {
Test6 t = new Test6();
t.met1(10, 6);
}
}

The more specific match (long) is choosen and the answer "long" is printed.

But on the following:

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 Test4 {
public static void main(String[] args) {
Square r = new Square();
System.out.println(r.area(5, 4));
}
}

generates the following error:
reference to area is ambiguous, both method area(int,int) in Rect
angle and method area(long,long) in Square match

I don't understand why the more specific match rule doesn't apply in this case. The area method should be overloaded and the Square class should contain both of the area methods, so that it reduces to the same situation as in the first example.

Clearly, I don't understand something correctly
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code :


You are specifically calling met1() method . You have two different methods met1() and met2() . This is not overloading .
Change the name of methods to a common name say met() and then call t.met(10,6)
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks OK and compiled OK for me on javac 1.5.0

What version of javac are you using?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lavjeet:

You are correct about Test6. What did you get for Test4? What version of Java?

I got no errors and output was 20.

Mike
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should explain why I ask the version of javac.

There is an old version of the "more specific" rule that was replaced with javac 1.4.2

The old rule was:


The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:

* T can be converted to U by method invocation conversion.
* Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.




While an int can be converted to a long, a Rectangle cannot be converted to a Square without an explicit cast.


This rule quietly changed with javac 1.4.2. It now reads:


The precise definition is as follows. Let m be a name and suppose that there are two member methods named m, each having n parameters. Suppose that the types of the parameters of one member method are T1, . . . , Tn; suppose moreover that the types of the parameters of the other method are U1, . . . , Un. Then the first member method is more specific than other if and only if Tj can be converted to Uj by method invocation conversion, for all j from 1 to n. A method is strictly more specific than another if and only if it is both more specific and the signatures of the two methods are not identical.

 
Sim Kim
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Me too got 20.
I use jdk1.4
Its simple overloading example . There is no overriding involved . So it will use Parent class method.
 
Bill Nelsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the first example, that was a simple typo (met1 and met2) that somehow slipped in.

Also thanks for the explanation, I found the original problem on a sample exam and then verified it on my local machine (which happened to have a JDK 1.3.1 compiler, where it give me the error. I subsequently ran it on the JDK 1.4.2 compiler and got the answer 20.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic