my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Specific match rule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Specific match rule" Watch "Specific match rule" New topic
Author

Specific match rule

Bill Nelsen
Greenhorn

Joined: Aug 11, 2004
Posts: 27
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
Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
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)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
It looks OK and compiled OK for me on javac 1.5.0

What version of javac are you using?


Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Aug 06, 2004
Posts: 268
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

Joined: Aug 11, 2004
Posts: 27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Specific match rule
 
Similar Threads
ques from overloading
Method overloading Problem
NoSuchMethodError
Overloaded method
Inheritance Doubt