aspose file tools *
The moose likes Beginning Java and the fly likes Doubts about references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubts about references" Watch "Doubts about references" New topic
Author

Doubts about references

Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
I'm reading "Learning the Java Language" by Sun and there is something I don't understand about object's references. I took my time studying the following example, adaptaded by myself:

public class SuperClass {

public static void hiding() {
System.out.println("hiding SuperClass");
}//close hiding()

public void overriding() {
System.out.println("overrinding SuperClass");
}//close overriding()
}//close SuperClass

class SubClass extends SuperClass {

int value =23;
public static void hiding() {
System.out.println("hiding� SubClass");
}//close hiding()

public void overriding() {
System.out.println("overriding�SubClass");
}//close overriding()



public static void main(String [] args) {
boolean theSame;
SubClass obj = new SubClass();
SuperClass objSuper = obj; /* Questions 1 */
SuperClass otroSuper = new SuperClass();
otroSuper = obj;
System.out.println(theSame = otroSuper.equals(obj));
/* System.out.println(otroSuper.value); Question 2 */
otroSuper.hiding();
otroSuper.overriding();
/*----------------------------------------------------------------------*/
objSuper = (SuperClass)obj; /* Question 3*/
System.out.println(theSame = objSuper.equals(obj));
objSuper.hiding();
objSuper.overriding();
}//close main
}//close SubClass

I finally understood about overriding and hiding but I really confuse about the following question:

1: What is the difference between SuperClass objSuper = obj; AND SuperClass otroSuper = new SuperClass();
otroSuper = obj; ?

2: if otroSuper is equal to obj, why otroSuper.value generates a compile-time error?

3: What does this cast do? The output is the same. Sorry, I don't understand.

Could anyone give me a hand about object's references especially when using inheritance like the example above?
THANK YOU ALL FOR YOUR HELP!

P.S. English isn't my mother tongue!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

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
Enrique Villamizar
Ranch Hand

Joined: Jul 30, 2005
Posts: 93
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.
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
If this example was in a book, I think it's a pretty bad example for a learner to try and understand. If it was on a test, I'd say it was good ;-)


Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
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".

System.out.println(((SubClass)otroSuper).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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts about references