Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding overloading 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 "Doubt regarding overloading" Watch "Doubt regarding overloading" New topic
Author

Doubt regarding overloading

Varsha Patil
Greenhorn

Joined: Mar 23, 2005
Posts: 13
static void doSomething ( A x , A y ) {System.out.print("AA");}
static void doSomething ( A x , B y ) {System.out.print"AB");}

static void doSomething ( B x , A y ) {System.out.print("BA");}
static void doSomething ( B x , B y ) {System.out.print("BB");}
static void doSomething ( C x , C y ) { System.out.println("CC");}//line 4

public static void main(String[] args) {
doSomething(null,null);
}
}

The above code prints "CC" as the o/p.If we comment out line 4 then it prints "BB". I m confused, as why does not it takes the other two values or print "BB" from first?

Please help..
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
What relationship do the classes A, B, and C have to each other?
Varsha Patil
Greenhorn

Joined: Mar 23, 2005
Posts: 13
Sorry....I did not paste the entire code.Here it is :


class A { }

class B extends A { }

class C extends B {
static void doSomething ( A x , A y )
{System.out.print("AA");}
static void doSomething ( A x , B y ) {System.out.print("AB");}
static void doSomething ( B x , A y ){System.out.print("BA");}
static void doSomething ( B x , B y ){System.out.print("BB");}
static void doSomething ( C x , C y ) { System.out.println("CC");}

public static void main(String[] args) {
doSomething(null,null);
}
}
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Varsha,

this is the case of invocation of the most specific method. Please read this in JLS 15.12.2.2 Choose the Most Specific Method

Hope that helps.

Soumya.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt regarding overloading
 
Similar Threads
overriding question
Qn on function call ().
about overloading
what is the result of compiling & executing. doSomething(null,null);
Passing Null which mehod is called?