This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing subclass objects to overridden methods taking superclass args. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Passing subclass objects to overridden methods taking superclass args." Watch "Passing subclass objects to overridden methods taking superclass args." New topic
Author

Passing subclass objects to overridden methods taking superclass args.

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
I have read this :
http://www.coderanch.com/t/266736/java-programmer-SCJP/certification/Overload-method and this is a well-known comment :
"which method to be called is decided by the compiler and the compiler knows only about the reference type it has no knoweldge about the actual object it refers to"

I can see what is happening in the code below but don't think the comment above is sufficient to explain it. Why does not (String) and (Integer) raise a compile error ? Why don't I need to do the upcast ?
Thank you.


[ October 12, 2008: Message edited by: Graeme Byers ]
[ October 12, 2008: Message edited by: Graeme Byers ]
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
for Integer ,there is "public void bMethod (Integer intObj)",for String ,because String is-a Object,so it can upcast


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Please read the question and the code - I know all of that.

As the quote states : "it's the class of the reference" that matters.
Well in the (String) example the class is java.lang.String so why can a method that takes a reference of class Object handle it (even if String is a subclass of Object) , why no "Symbol not found" because there is no void aMethod (String s) {}.
Why do not I need to explicitly upcast String to Object before calling aMethod ?
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 40


Umesh rawat <br />Rmw -2019

http://100javaquestions.blogspot.com
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Hi Graeme,


Why does not (String) and (Integer) raise a compile error


t.aMethod(new String("A")) ; // Object java.lang.String
Compile and run. It call method aMethod which takes Object parameter because String IS-A Object or we can say that there is inheritance hierarchy between Object and String

t.bMethod(new Double (1.0)) ;
compiler error. It call method bMethod with Integer parameter, in here code will break because Integer IS-A not Double or vice versa.
[ October 13, 2008: Message edited by: Vierda Mila ]

SCJP 5
Zdenek Pine
Greenhorn

Joined: Oct 06, 2008
Posts: 9
Hi Graeme,
I had also problem to understand this theme, but I think up this Pub Payment Rule: you can overpay, but not underpay. So in this case, if aMethod required Object, you can pay by String, because String is Object, even more then Object, so actually you tip (bakshish) the staff of aMethod. So everybody is content. But if you send Object to aMethod(String), there will be complains, because Object is not big enough. In java slang, Object is not String. And if you send Integer to aMethod(Double), you pay by not convertible currency, because Integer and Double is not in sub/super relationship (they are not convertible/castable in any case). So the waiter will refuse you payment and you will be in trouble.
So please go to a pub only if you have the right currency and enough money.
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
@Zdenek Pine
Funny Example.

Always goto the sun documentation and see the inheritance hierarchy and do some digging and you would yourself know why a Double cannot be cast into a Integer. Infact an Integer cannot also be cast to Double.
Both these wrappers inherit from java.lang.Integer and are kind of brothers(sort-of-leaves-of-a-tree), and you cannot cast sideways in inheritance hierarchy.
But String IS-A Object, or a child or Object, so can be casted.
And why an Object cannot be casted to String?
Because Object is a kind-of bigger bucket than String since it is higher in hierarchy than String.
So String(a smaller bucket) can be placed in an Object(bigger bucket) but not vice-versa.
Please go through this thread too.


Preparing for SCJP 6.0
Zdenek Pine
Greenhorn

Joined: Oct 06, 2008
Posts: 9
Hi Paul,
thanks for you explanation. You are right. One can think Object as bucket of allowed possibilities.
So it is better to think java function or left side of assignment as a cigarette vending machine. If machine accepts Object - yupee! you can pay by everything. If it accepts Number, you can pay by Integer. But not by String, because it is different bucket of possibilities.
So my nicotine need is now gratified. Thank you.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Passing subclass objects to overridden methods taking superclass args.
 
Similar Threads
for ( ; ; )
Can anybody explain the error msg when run?
explain the out put
aMethod (Integer ) is not defined
why am I getting verify error ???