#Relationship between variables and their assigned objects:

1 messages · Page 1 of 1 (latest)

heady glacierBOT
#

<@&987246399047479336> please have a look, thanks.

#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

versed mirage
#

In short, I don't understand the significance of distinguishing an object from its class of variable

#

e.g.,
r2.getCurrentRow() seems to get the current row of the RookS object that r2 refers to

#

but r2.currentRow accesses the currentRow attribute of its PieceS variable?

signal kelp
#

also your rooks is returning its local field, not its super

versed mirage
#

I didn't write this btw I just don't understand why it would do this based on my understanding of java lol

versed mirage
signal kelp
#

since u got a local field there, it will return that, not the super

versed mirage
#

right ok

and calling r2.currentRow accesses the super attribute instead?

#

(this is r2 btw: PieceS r2 = new RookS(8,3))

signal kelp
#

alter that code and see what it prints

#

( also its kinda weird code aswell , school example ? )

versed mirage
#

when r2.getCurrentRow() is called, it makes sense that it would access the RookS instance variable
but why should rn.currentRow access that of its superclass? I assume that this has something to do with the fact that r2 is a PieceS variable with a RookS object assigned

versed mirage
#

it's meant to demonstrate shadowing

versed mirage
#

i have theories is all

signal kelp
#

rn.curentRow has a local variable aswell with that name

#
public class RookS extends PieceS {
    public int currentRow;
    public int currentColumn;
    public RookS(int currentRow, int currentColumn) {
        super(currentRow, currentColumn);
    }
    public int getCurrentRow() {
        return this.currentRow;
    }
}
#

is what is happening

versed mirage
#

so when r2.currentRow is processed

#

what is interpreted?
it seems like java thinks I'm referring to the currentRow in the superclass

#

even though like you say, r2 has a local variable with the same name

signal kelp
#

print out the object's actual class

versed mirage
#

true

#

is there a standard method for doing that

#

i don't think i've needed to do that as of yet

#

only used instanceof

signal kelp
#

at the moment don't overthink on it, this whole example is weird

versed mirage
#

added this line

#

System.out.println(r2.getClass().toString());

#

it says

signal kelp
#

rooks shoudlnt even have the 2 fields in a real example

versed mirage
#

class RookS

versed mirage
#

but I have an exam tomorrow for this subject haha
and I bet they're gonna try and trip me up on the particulars of the object-variable relationship

signal kelp
#

hoping u did the basic examples, did u happen to see the dog and cat example ?

versed mirage
#

"the" dog and cat example?

signal kelp
#

dog/cat extends animal

versed mirage
#

yeah, I recall

#

i'm familiar with inheritance and the general rules

it's just this one example

signal kelp
#

right, in there is usually a method called makeSound

#

so if u place String sound = "" in animal; String sound ="woof" in dog, and String sound="im pure evil" in cat; then your doing the same thing as in your example

versed mirage
#

right that makes sense

do you know anything about the implications of this though?

// this
RookS r1 = new RookS(4, 3);

// versus this
PieceS r2 = new RookS(4, 3);
#

are there any general rules about how to interpret this sort of thing when the variable type differs from the obj type

#

I suppose accessing the attribute directly takes the one from the superclass or sth

signal kelp
#

might explain your last example a bit

versed mirage
#

nice cool

#

thanks fa285634

signal kelp
#

maybe a bad explanation on that code is -> i know your a pieceS, but i dont care what extended things u got added. Your going to do stuff from pieceS anyway

#

or i know your a car, even if your hybrid, fossile or solar. You'r going to start()