differences between object and object reference?

M

Matt

What's the differences between object and object references? To me, it
seems the same, but I am sure there are differences.

Student s = new Student("Joe",20);

s is an object of class Student. Can we say s is an object reference
of class Student?

Please clarify. thanks!!
 
L

Lee Weiner

What's the differences between object and object references? To me, it
seems the same, but I am sure there are differences.

Student s = new Student("Joe",20);

s is an object of class Student. Can we say s is an object reference
of class Student?

Please clarify. thanks!!

s is not an object, it's a variable which contains a reference to an object.
Objects don't have names, just types and locations in memory (and, of course,
fields and methods). Read your statement as: Create a new Student object in
memory, initializing it with the data sent as arguments to a constructor, and
when created, assign a reference to that object to the Student variable s. s
is a reference or object type variable which may reference a Student object or
an object of any subclass of Student.

Take another statement: int x = s.getValue();

Read this statement as "Go to the object referenced by variable s and execute
its getValue() method. Assign the return from that method to the int variable
x."

Lee Weiner
lee AT leeweiner DOT org
 
M

Matt

Thanks, Lee!!

Why subclass variable can't reference a superclass object?

class A
{ //etc...
}

class SubClassA extends A
{ //etc...
}

I know subclass variable can't reference a superclass object,
but only superclass variable can reference a subclass object.
But I don't understand the rationale.

Please advise. Thanks!
 
V

VisionSet

Matt said:
Thanks, Lee!!

Why subclass variable can't reference a superclass object?

class A
{ //etc...
}

class SubClassA extends A
{ //etc...
}

I know subclass variable can't reference a superclass object,
but only superclass variable can reference a subclass object.
But I don't understand the rationale.

What you are saying, to use your example classes, is that you can do:

A a = new SubClassA();

but you can't do:

SubClassA subA = new A();

Because how can subA fully describe an Object of class SubClassA without
being constructed from it?

The Former example is possible because SubClassA does fully describe the
class 'A'.

Do a search under 'RTTI' (run time type identification) and 'polymorphism'.
 
C

chris

Matt said:
Thanks, Lee!!

Why subclass variable can't reference a superclass object?

What do you mean by "subclass variable"? A variable declared to have type
class A
{ //etc...
}

class SubClassA extends A
{ //etc...
}

I know subclass variable can't reference a superclass object,
but only superclass variable can reference a subclass object.
But I don't understand the rationale.

I've tried combining various possible interpretations of "subclass
variable" and "superclass object", and for all of them this statement id
patently false. So apparently I don't understand the proposition, never
mind the rationale.

Can you give us a more detailed example?
 
A

Arvind

Matt,
This question has been answered assuming the question was other way
around in another thread..please check out..

Arvind
"I dont have any quotes..."
 
G

Guest

Thanks, Lee!!

Why subclass variable can't reference a superclass object?

All dalmatians are dogs, but not all dogs are dalmatians. IFF you know
the runtime type of the Object, you may cast it.

Dog dog = new Dalmatian();
Dalmatian dalmatian = (Dalmatian) dog;

This is how collections work prior to 1.5

HTH,
La'ie Techie
 
Joined
May 20, 2010
Messages
1
Reaction score
0
Object And Reference

String a="Hello";
String b="HI";

String x=new String("ABC");
String y=new String("XYZ");

Can anyone please tell how many reference and how many object are there in four different expressions....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top