• 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

overloaded method call rules

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this was a questiion in wizlabs


given the out put is SuperSuperBase

But is it not the runtime object that has to be considered for the method calls and not the reference type ? in which case b1.print() should invoke method in Base class and print Base and c1.print() should invoke Derived classe's print and print Derived.
out put being SuperBaseDerived

Can any let me know if am correct or the wizlabs answer is ?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajashekar,

In case of overriden methods, the actual object at the run time is considered and the method in the actual Object's Type is invoked.

But for the overloaded methodes the mapping happens at the compile time, and the complier decides which method to be invoked based on the reference type(ie, the method in the reference type will be invoked.).

Hope this clears your doubt.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajasekhar:

First: are you sure this code compiles, because it will give you at least 7 compiler errors.

This will be the correct code:



Well, once fixed, the wizlab�s answer is correct, it�s all about polymorphism. Take a look in the main method, you are creating a SuperBase object with a reference variable a1 of type SuperBase, there would not be any problem with that, but also you are creating a Base object with the reference variable b1 of type SuperBase too (here is the first polymorphic thing), and finally you are creating a Derived object with a reference variable c1 of type Base (here is the second polymorphic thing).

Then you are calling the print method inside the a1 variable, here there would not be problem, then you are calling the print method inside the b1 variable, but here remember that b1 is of type SuperBase, so b1 just know its own print method, the one inside of SuperBase (if you want the print method of Base class you must down-cast b1) and you are passing it a Derived object, but remember that Derived is grandchild of SuperBase, so a Derived object IS-A SuperBase object too. Finally you are calling the print method inside the c1 variable, remember c1 is of type Base, so c1 just know its own print method which takes a Base object as parameter and the one inherited from SuperBase, and (again) you are passing a Derived object and remember that a Derived object is a child of Base, so a Derived object IS-A Base too. Then the JVM decides that it will use the print method which takes a Base object.

Do you get it?

I hope this help you to understand it.

Regards,
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Alajandro,

My doubt is

a1 is of type 'SuperBase",but in Superbase there is no print method which
recievs an object of type "Base",then how a1.print(new Base() is being executed?and printing 'Super"

b1 is of type "Superbase"and it doesnt have a print method which takes a Derived object,then how b1.print(new Derived())is being compiled and printing 'Super"again,same is the doubt with c1.print(new Derived()); too.


Confuses in polymorphism and inheritance,pl clarify
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vatsalya:

As I told in my last post, a1 only know the method which takes a SuperBase object and you are passing it a new Base object, but remember that Base extends SuperBase, so any Base object will pass the IS-A test for SuperBase, I mean a Base object always will be a SuperBase object too, so the compiler use this Base object as a SuperBase object in order to complain with the print method. Is the same in the three cases, b1 only knows the print method which takes a SuperBase object and you are passing it a Derived object, and Derived extends Base and Base extends SuperBase, so a Derived object always will be a Base object and a SuperBase object too, the compiler doesn't have any problems to see your Derived object as a SuperBase object to complain with the method. Finally c1 knows the print method which takes a SuperBase and the print method which takes a Base object and you are passing it a Derived object, so since the Derived object extends Base it is a Base object too, the compiler decides to use the print method which takes a Base instead a SuperBase because the implicit cast is shorter from a Derived to a Base than to a SuperBase.

Do you get it? I hope this help you to understand, let us know if you need more explanation for someone else can help us to explain you this.

Regards,
 
Rajasekhar Devi Reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thq for the reply guys

But , A book on SCJP says this about overriding rules,




In the preceding code, the test class uses an Animal reference to invoke a method
on a Horse object.



Now isin't the wizlabs question in my earlier piost similar to this explanation here.. so b1.print() and c1.print() should thy not run the methods in their Base and Deriver objects respectively ???
 
Alejandro Galvan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajasekhar:

Yes, you are right, but in your first example you are overloading not overriding, so the rule is not the same as in the last example.

As Varalakshmi said: "In case of overriden methods, the actual object at the run time is considered and the method in the actual Object's Type is invoked". So, it's not the same case than in your first example, the only similiar things between this both examples is you are calling a method in a reference variable with distinct type of object associeted, but is not the same rule.

Hope the clear your doubt.

Regards,
reply
    Bookmark Topic Watch Topic
  • New Topic