This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1: What is the difference between SuperClass objSuper = obj; AND SuperClass otroSuper = new SuperClass(); otroSuper = obj;
No difference. You initialize otroSuper to a new SuperClass() and immediately discard that new object and reassign the obj object to the same variable.
2: if otroSuper is equal to obj, why otroSuper.value generates a compile-time error?
SuperClass has no variable named "value"
3: What does this cast do? The output is the same.
Maybe just to prove that even if you explicitly cast to SuperClass (normally unnecessary), the JVM still sees it as the SubClass object.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Jul 30, 2005
Thank you Marilyn, you're very kind!
About question 2: if SuperClass has no variable named "value" and SubClass has one why Java says they�re equal? I mean, a SuperClass� object isn�t equal to a SubClass, Am I right? That�s because SuperClass has no variable named "value" but SubClass does, so otroSuper is different to obj. I know there is something I don�t get it. Is otroSuper really equal to obj? -( I don�t think so)- is Java wrong?
Could you give me a plain explanation, please? After that I�m going to bed, I promise! In Colombia is 2:20 A.M.
You'll need to override equals() methods on both classes to get true results. There may be some debate in the community, but I'd suggest that no object should ever report true for equals an instance of any superclass or subclass.
Equals methods usually look something like:
The Javadoc also advises you to override hashcode. It's probably a good idea to override compareTo() as well, just in case somebody wants to put your objects into a sorted collection.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marilyn de Queiroz
Joined: Jul 22, 2000
Why you can't see value is not such an simple question to answer. Note that if you downcast otroSuper rather than upcast it, you can compile and see the value of "value".
Is otroSuper really equal to obj?
Note that even though you assigned the object "obj" to a SuperClass-type variable named "objSuper", you assigned the same object "obj" to a SuperClass-type variable named "otroSuper". The Object "obj" is still the same object being referenced by two different names.
If you call your dog "dog" or you call your dog "pedro", it is still the same dog. Same thing here.
Thus, obj equals obj, objSuper equals obj and otroSuper equals obj because it is the same object being referenced by different names.
Because you are using a SuperClass variable (member variables are not polymorphic, only instance methods are polymorphic), you cannot access any variable which does not exist in the SuperClass without casting.
I think this is pretty difficult stuff for a beginner. [ August 07, 2005: Message edited by: Marilyn de Queiroz ]