Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding overloading

 
Varsha Patil
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What relationship do the classes A, B, and C have to each other?
 
Varsha Patil
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic