File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding overloading

 
Varsha Patil
Greenhorn
Posts: 13
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic