aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Problem 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 "Overloading Problem" Watch "Overloading Problem" New topic
Author

Overloading Problem

Ravi Bhushan Ratnakar
Greenhorn

Joined: Apr 14, 2008
Posts: 3
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

What happened when you tried running it? that's the best way to see what happens.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: Apr 14, 2008
Posts: 3
Sorry to say, but there isn't any compilation error. I am using jdk1.6 update 5 version
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

well it did work fine for me and it invokes

method.

I am still not able to figure out how this thing works


-kolli
Ravi Bhushan Ratnakar
Greenhorn

Joined: Apr 14, 2008
Posts: 3
This is the result which is bothering me. Why that result is comming, not able to figure out. Anyway thanks
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

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

Joined: Dec 22, 2004
Posts: 142
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

Joined: Aug 06, 2007
Posts: 486
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..


Thanks<br />Dinesh
anuj bhatnagar
Ranch Hand

Joined: Aug 06, 2007
Posts: 31
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.


thanks & regards,<br />Anuj Bhatnagar
 
 
subject: Overloading Problem