*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloaded method call rules 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 "overloaded method call rules" Watch "overloaded method call rules" New topic
Author

overloaded method call rules

Rajasekhar Devi Reddy
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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 ?
Varalakshmi Ramanarayan
Ranch Hand

Joined: Oct 28, 2007
Posts: 108
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.


SCJP 5 - 87%<br />Next SCWCD
Alejandro Galv�n
Greenhorn

Joined: Nov 17, 2007
Posts: 17
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,


SCJP5.0
vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
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
Alejandro Galvan
Ranch Hand

Joined: Jan 02, 2008
Posts: 48
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,


SCJP 5.0<br />Preparing SCWCD
Rajasekhar Devi Reddy
Greenhorn

Joined: Dec 21, 2007
Posts: 29
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

Joined: Jan 02, 2008
Posts: 48
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,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overloaded method call rules
 
Similar Threads
confused with Overloading rules
(Solved!) Inheritance, polymorphism and overriding.
Super class method
Ovverriding
can any body explain me output of these program