differences between object and object reference?

Discussion in 'Java' started by Matt, Apr 10, 2004.

  1. Matt

    Matt Guest

    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!!
     
    Matt, Apr 10, 2004
    #1
    1. Advertising

  2. Matt

    Lee Weiner Guest

    In article <>, (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
     
    Lee Weiner, Apr 10, 2004
    #2
    1. Advertising

  3. Matt

    Matt Guest

    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!

    (Lee Weiner) wrote in message news:<c3Hdc.1939$nK1.17579@attbi_s54>...
    > In article <>, (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
     
    Matt, Apr 25, 2004
    #3
  4. Matt

    VisionSet Guest

    "Matt" <> wrote in message
    news:...
    > 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
     
    VisionSet, Apr 25, 2004
    #4
  5. Matt

    chris Guest

    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
    /k/ Embedded Java Solutions
     
    chris, Apr 25, 2004
    #5
  6. Matt

    Arvind Guest

    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 <> wrote in message news:<c6hbg8$m8k$>...
    > 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?
     
    Arvind, Apr 28, 2004
    #6
  7. 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
     
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=, May 25, 2004
    #7
  8. Matt

    Kapil75588

    Joined:
    May 20, 2010
    Messages:
    1
    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....
     
    Kapil75588, May 20, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mosfet
    Replies:
    3
    Views:
    392
    Chris Thomasson
    May 11, 2007
  2. Replies:
    6
    Views:
    388
    Bruclee
    Dec 10, 2008
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    516
    Home_Job_opportunity
    Jan 8, 2009
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    605
    Home_Job_opportunity
    Jan 14, 2009
  5. Eric Snow
    Replies:
    0
    Views:
    530
    Eric Snow
    Aug 11, 2011
Loading...

Share This Page