This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Consider this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Consider this code" Watch "Consider this code" New topic
Author

Consider this code

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Hi,



output:-T2,T3,T2,T2,T3,T2
What is going on in this code?
Please help me with this.

Thanks
Sandhi
[ March 14, 2008: Message edited by: sandhi mridul ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40



What is t2f() and t3f()?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
I am sorry Henry.
While typing I missed the imp thing -the dot operator.
I have corrected it.

Thanks
But still need the ans
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

First of all, you do know that you are not overriding the f() method right? You are overloading it. With overloading, the type of reference is actually important -- as it is used to determine the method to call. And since the method is determined at compile time, the type of reference is important.

A T2 reference only has a single f() method, while a T3 reference has two overloaded f() methods. Meaning, it doesn't matter whether you pass a T2 or T3 reference to a T2 reference, it will be treated as a T2 reference. But it does matter whether you pass a T2 or T3 reference to a T3 reference, as it will be used to determine which overloaded method to call.

t2.f(t3); --> T2, because a T2 reference only has a single f() method
t3.f(t3); --> T3, because the overloaded f(T3) method is called
t2.f(t2); --> T2, because a T2 reference only has a single f() method
t3.f(t2); --> T2, because the overloaded f(T2) method is called
t3.f((T3)t2); --> T3, because the overloaded f(T3) method is called
t2.f((T3)t3); --> T2, because a T2 reference only has a single f() method

Henry
sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Thanks Henry.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Consider this code