my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method invocations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method invocations" Watch "Method invocations" New topic

Method invocations

Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Please read code1,2,3 below:-

Code 1:

class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {m(null);}



class GFC200 {}
class GFC201 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}

***Compile time error

Code 3:
class GFC202 {} class GFC203 extends GFC202 {}
class GFC204 {
static void m(GFC202 x) {System.out.print("GFC202");}
static void m(GFC203 x) {System.out.print("GFC203");}
public static void main(String[] args) {m(null);}

Output: GFC203

I did not understand the o/p of 2 and 3.
In the code2 both the String as well as GFC200 classes extend Object class implicitly then where is the ambuiguity and hence why does it give compile time error.

In the code 3 how does GFC203 get printed or how is it more specific then GFC202 class.

If anybody could explain it would be appreciated.



--------------------------------<br />SCJP1.4
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
The rules are that the compiler will resolve overloaded method calls to the most derived class and ambiguity causes compiler errors.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
To get good grasp of this concept. I would suggest you go through the excellent article written by Corey . Here is the link

Hope that helps ya.

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
I agree. Here's the link:
subject: Method invocations
It's not a secret anymore!