• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object references question  RSS feed

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Dog extends Animal, and Pomeranian extends Dog, then would it be accurate to say that Dog is an Animal reference that refers to a Pomeranian in the following code snippet?




I understand the concept of subclassing and extending objects, but I get really hung up on semantics. I read in different beginner books about "object references" and then it gets me all confused again.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted does not make sense. It looks like you put double type declarations where there should be a type declaration followed by a variable declaration.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote: would it be accurate to say that Dog is an Animal reference that refers to a Pomeranian


No. In that snippet you have two different Objects, and two different references:

In this one you have an Animal object [because of the new Animal() call], and an Animal reference [because of the Animal name on the left side of the equal sign]. The name of that reference is 'Dog', but that name is arbitrary, and has no relationship to the class Dog.


Here you have a Dog object (because of the new Dog() call], and a Dog reference [because of the Dog on the left side of the equal sign]. The name of the reference (the variable name) is Pomeranian, but that has no relationship to any Pomeranian class which may or may not extend Dog.
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so how would the code go for a Dog object that is an Animal reference to point to a Pomeranian?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vonique Leary wrote:Okay, so how would the code go for a Dog object that is an Animal reference to point to a Pomeranian?

I'm not sure what that means.

There are two types to consider:
  • The type of the object.
  • The type of the reference that points to the object.


  • In code, it can look like this...

    The reference type must be either the object type or a supertype of that object. The variable name (as Steve pointed out) is whatever you decide to call it. It does not influence the type.

    So you can do this...

    Here, you are creating a Pomeranian object (which is also a Dog, as well as an Animal), and pointing to that object using a reference of type Animal, which is assigned to a variable called "myDog."
     
    Alex Hurtt
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marc weber wrote:
    Vonique Leary wrote:Okay, so how would the code go for a Dog object that is an Animal reference to point to a Pomeranian?

    I'm not sure what that means.

    There are two types to consider:
  • The type of the object.
  • The type of the reference that points to the object.


  • In code, it can look like this...

    The reference type must be either the object type or a supertype of that object. The variable name (as Steve pointed out) is whatever you decide to call it. It does not influence the type.

    So you can do this...

    Here, you are creating a Pomeranian object (which is also a Dog, as well as an Animal), and pointing to that object using a reference of type Animal, which is assigned to a variable called "myDog."


    Also worth noting is that by doing this, you can only invoke methods defined by Animal on your myDog reference. So if Pomeranian defines some method that Animal does not have...say 'beSmallAndObnoxiouslyCute()' you will not be able to do this:



    Now lets say for the sake of argument that Animal does define some non-final, non-private, overridable method called beSmallAndObnoxiouslyCute() and your Pomeranian subclass overrides that Animal beSmallAndObnoxiouslyCute() implementation with its own. Now you can do what I showed in the code snippet up above but it may surprise you to see (or then again it might not) that at runtime, the Pomeranian overridden version of the method gets invoked, not the Animal implementation.
     
    Vonique Leary
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now lets say for the sake of argument that Animal does define some non-final, non-private, overridable method called beSmallAndObnoxiouslyCute() and your Pomeranian subclass overrides that Animal beSmallAndObnoxiouslyCute() implementation with its own. Now you can do what I showed in the code snippet up above but it may surprise you to see (or then again it might not) that at runtime, the Pomeranian overridden version of the method gets invoked, not the Animal implementation.


    I did remember that, about the overriding method that gets invoked rather than the Animal one. Good review, though. Thanks.
     
    Vonique Leary
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here, you are creating a Pomeranian object (which is also a Dog, as well as an Animal), and pointing to that object using a reference of type Animal, which is assigned to a variable called "myDog."



    So, myDog is an Animal reference that points to a Pomeranian?

    I don't know why it is so hard for me to wrap my brain around that concept???

    How would you describe it using everyday objects? (if there is a way......)


    Or if you can point me to another post that will explain it.

    Thanks, Von
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try Sierra and Bates, Head First Java 2/e, page 199.
     
    Vonique Leary
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Campbell. I think I have that book, so I will do that!

    Vonique
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome Be sure to tell us whether that helps.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!