Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on Overloading

 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from Dan's mock exams.

class GFC205 {}
class GFC206 extends GFC205 {}
class GFC207 extends GFC206 {
static void m(GFC205 x, GFC205 y) {System.out.print("GFC205, GFC205");}
static void m(GFC205 x, GFC206 y) {System.out.print("GFC205, GFC206");}
static void m(GFC206 x, GFC205 y) {System.out.print("GFC206, GFC205");}
static void m(GFC206 x, GFC206 y) {System.out.print("GFC206, GFC206");}
public static void main(String [] args) {
GFC207 gfc207 = new GFC207();
m(gfc207,gfc207);
}
}

Prints:GFC206,GFC206



class GFC211 {}
class GFC212 extends GFC211 {}
class GFC213 extends GFC212 {
static void m(GFC211 x, GFC211 y) {System.out.print("GFC211, GFC211");}
static void m(GFC211 x, GFC212 y) {System.out.print("GFC211, GFC212");}
static void m(GFC212 x, GFC211 y) {System.out.print("GFC212, GFC211");}
static void m(GFC212 x, GFC212 y) {System.out.print("GFC212, GFC212");}
static void m(GFC211 x, GFC213 y) {System.out.print("GFC211, GFC213");}
public static void main(String [] args) {
GFC213 gfc213 = new GFC213();
m(gfc213,gfc213);
}
}

Prints: Compile-time error.


My question:
1)How is that in the first case the method m(GFC206,GFC206) more specific than the other methods?
2)Why is that in the second case m(GFC212,GFC212) not more specfic than any other methods?
Please explain

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this sense, "more specific" basically means further removed from the base class, Object. So with each level of inheritance, we can say that a type is getting "more specific."

In the first example, GFC207 extends GFC206, which extends GFC205, which extends Object. Therefore, GFC207 is more specific than GFC206, which is more specific than GFC205, which is more specific than Object.

We're calling a method passing GFC207 references. Because of inheritance, a GFC207 is also a GFC206, and a GFC205, and an Object. So depending on the context, any of these methods could work. But because we have a choice, we want to select the best fit -- that is, the method that takes arguments closest to the GFC207's we have.

In this sense, m(GFC205, GFC206) is more specific than m(GFC205, GFC205) because it treats one of the parameters as a GFC206, which is more specific than a GFC205. But ultimately, m(GFC206, GFC206) is invoked because this is the most specific argument list that fits the parameters we're passing.

The second example is similar, except that it is not clear which method provides the best fit. On one hand, m(GFC211, GFC213) has an exact fit for one of the parameters, but the other argument type is GFC211, which isn't very specific to what we're passing because GFC213 is a subclass of a subclass of GFC211. On the other hand, m(GFC212, GFC212) might be a better choice because each argument type is an immediate superclass to the GFC213 references we're passing. Which would be better? Based on our argument types, we can't know, and so we can't say that any of these methods is "more specific."
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should check out this link:
http://www.coderanch.com/t/247191/java-programmer-SCJP/certification/Method-Overloading

How is that in the first case the method m(GFC206,GFC206) more specific than the other methods?

GFC205 can accept any value that GFC206 can accept, but GFC206 cannot accept all values that GFC205 can accept. For example, a parameter of type GFC206 cannot accept arguments of type GFC205. This makes GFC206 more specic than GFC205.

2)Why is that in the second case m(GFC212,GFC212) not more specfic than any other methods?

Because m(GFC211, GFC213) is more specific than m(GFC212,GFC212) in the second parameter but less specific in the first parameter.


[ December 17, 2004: Message edited by: Mike Gershman ]
[ December 17, 2004: Message edited by: Mike Gershman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic