• 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

Look at this code (about what instance method is choosen to run)

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this very confusing:

The output is: C. This is what I expected - becouse the runtime type of c1 is C.
But look at this code (note that the only changes are the argument types of the m1 methods):

This time the output is A!! Seems like in Java if you have a method that the parameter type has a IS-A relationship with the class that has the method, it chooses to run the method of the reference type (ignoring the actual runtime type of the object the reference refers to).
Am I right on my guess or there is a better / right explanation for this behavior?
[ October 29, 2003: Message edited by: bengt hammarlund ]
[ October 29, 2003: Message edited by: bengt hammarlund ]
[ October 29, 2003: Message edited by: bengt hammarlund ]
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bengt
I had the exact same doubt a few days ago. See this post:
https://coderanch.com/t/243522/java-programmer-SCJP/certification/Dan-Questions-Dynamic-Method-Invocation
I've somehow 'swallowed' the concept but never really understood why they did things like they did.
Cheers
Harwinder
 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harwinder!
Thanks!
I got it now! The difference is that in the first example the methods are been overriden, and on the second they're been overloaded!! I just noticed that now! DUH!!!
Now the explanation is: the methods that are been overloaded are totally different methods. So if you have an A reference, the ONLY method you got is void m1(A a)! The others are been defined on the other classes that extend the A class.
When you create an object and assign it to a superclass reference type (eg.: A c1 = new C() then you're saying to the compiler "create an object of type C, but assign it to a reference that gives me only the interface found on the A class declaration".
In runtime, Java knows that the c1 reference of type A is actually an object of type C, but you'll only have access to the methods found on A declaration. You can get access to the other methods found on C class by making a explicit cast like this: ((C)c1).m1
I hope you got it now. Thanks anyway!!
[ October 29, 2003: Message edited by: bengt hammarlund ]
[ October 29, 2003: Message edited by: bengt hammarlund ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bengt
I do understand how it works. If you read the last msg in the link I gave you above, you'll know that my main question is on the way this is designed ... it seems kinda inconsistent. Well nevermind ... concentrate on your exam.
Cheers
Harwinder
 
reply
    Bookmark Topic Watch Topic
  • New Topic