Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > differences between object and object reference?

Reply
Thread Tools

differences between object and object reference?

 
 
Matt
Guest
Posts: n/a
 
      04-09-2004
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!!
 
Reply With Quote
 
 
 
 
Lee Weiner
Guest
Posts: n/a
 
      04-10-2004
In article <(E-Mail Removed) >, http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt) wrote:
>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
 
Reply With Quote
 
 
 
 
Matt
Guest
Posts: n/a
 
      04-25-2004
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!

(E-Mail Removed) (Lee Weiner) wrote in message news:<c3Hdc.1939$nK1.17579@attbi_s54>...
> In article <(E-Mail Removed) >, (E-Mail Removed) (Matt) wrote:
> >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

 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      04-25-2004

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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'.

--
Mike W




 
Reply With Quote
 
chris
Guest
Posts: n/a
 
      04-25-2004
Matt wrote:

> Thanks, Lee!!
>
> Why subclass variable can't reference a superclass object?


What do you mean by "subclass variable"? A variable declared to have type
<subclass>? Or maybe a variable declared in class <subclass>? Or some third
thing ... Ditto for "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.


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?

--
Chris Gray (E-Mail Removed)
/k/ Embedded Java Solutions

 
Reply With Quote
 
Arvind
Guest
Posts: n/a
 
      04-28-2004
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..."


chris <(E-Mail Removed)> wrote in message news:<c6hbg8$m8k$(E-Mail Removed)>...
> Matt wrote:
>
> > Thanks, Lee!!
> >
> > Why subclass variable can't reference a superclass object?

>
> What do you mean by "subclass variable"? A variable declared to have type
> <subclass>? Or maybe a variable declared in class <subclass>? Or some third
> thing ... Ditto for "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.

>
> 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?

 
Reply With Quote
 
=?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
Guest
Posts: n/a
 
      05-25-2004
On Sun, 25 Apr 2004 12:53:40 -0700, Matt wrote:

> 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

 
Reply With Quote
 
Kapil75588 Kapil75588 is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-20-2010
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....
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
code object differences between 2.7 and 3.3a Eric Snow Python 0 08-11-2011 12:38 AM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-14-2009 03:51 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-08-2009 04:31 PM
The differences between the object and the pointer points to object kailang.deng@gmail.com C++ 6 12-10-2008 12:28 PM
Differences between "class::member" & "object.member" Jian H. Li C++ 4 02-13-2004 10:54 AM



Advertisments