This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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


Win a copy of Head First Android this week in the Android forum!
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.
 
jQuery in Action, 3rd edition
 
subject: Doubt regarding overloading
 
It's not a secret anymore!