• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading Problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried running it? that's the best way to see what happens.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to say, but there isn't any compilation error. I am using jdk1.6 update 5 version
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic