Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading Problem

 
Ravi Bhushan Ratnakar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class CA
{
public void show(CA a)
{
System.out.println("CA of CA");
}
public void show(CB b)
{
System.out.println("CB of CA");
}
}


class CB extends CA
{
public void show(CA a)
{
System.out.println("CA of CB");
}
public void show(CB b)
{
System.out.println("CB of CB");
}


}



public class OvrOvl
{
public static void main(String args[])
{

CA aob=new CB();
aob.show(null);
}
}

what will be output of this programme? And please do give detailed explaination of the answer. Thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried running it? that's the best way to see what happens.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code woun't compile, first and formost, since the method invokation show(null) is ambiguous. You would either have to use show((CA) null) or show((CB) null).
Then, the implementation executed will be that from CB, so the output will be "... of CB". What the dots become depends on the typecast you insert, see above.

Hope that helps,
Guido
 
Ravi Bhushan Ratnakar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to say, but there isn't any compilation error. I am using jdk1.6 update 5 version
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well it did work fine for me and it invokes

method.

I am still not able to figure out how this thing works
 
Ravi Bhushan Ratnakar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the result which is bothering me. Why that result is comming, not able to figure out. Anyway thanks
 
Karl Prenton
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At runtime the jvm chooses the most specific method, given the choice of two in this case. See here http://faq.javaranch.com/java/ScjpFaq#mostSpecific
 
fred rosenberger
lowercase baba
Bartender
Posts: 12184
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i believe that when you have thisyou are actually passing in a Object object. this is different that if you had
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

am I missing something? There is no no-argument show() method in eitherr of the two classes CA and CB ...
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass
public class OvrOvl
{
public static void main(String args[])
{

CA aob=new CB(); //line1
aob.show(aob);//line3
}
}


o/p -- CA of CB

**************************

When you pass

public class OvrOvl
{
public static void main(String args[])
{

CA aob=new CB();//line 2
aob.show(null);//line4
}
}

o/p -- CB of CB

Can anyone explain me the difference.
As far i know overriding deals with who's objet is cretaed in line2 CB object is created that is correct
but what about line 3 and line 4 not able to get..
 
anuj bhatnagar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation is very simple for this problem.
Since you are assigning sub class instance to superclass reference, and calling a method which is overridden by subclass, the sub class method will be called since the actual instance is of sub class.
Next is passing null to method
now as other rauncher rightly mentioned, passing null will call the most specific method of that object. Since there are two methos one takes the argument of super class and other takes the argument of sub class , subclass method will be called because it is more specific. Hence CB of CB will be the output.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic