#Relationship between variables and their assigned objects:
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
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?
also your rooks is returning its local field, not its super
I didn't write this btw I just don't understand why it would do this based on my understanding of java lol
in which line?
right ok
and calling r2.currentRow accesses the super attribute instead?
(this is r2 btw: PieceS r2 = new RookS(8,3))
alter that code and see what it prints
( also its kinda weird code aswell , school example ? )
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
yeah lol
it's meant to demonstrate shadowing
i've messed around with it a bit/seen what fixes it
I just don't definitively know what's causing this
i have theories is all
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
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
print out the object's actual class
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
at the moment don't overthink on it, this whole example is weird
rooks shoudlnt even have the 2 fields in a real example
class RookS
i understand, and I'd never shadow a superclass attribute like that lol
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
hoping u did the basic examples, did u happen to see the dog and cat example ?
"the" dog and cat example?
dog/cat extends animal
yeah, I recall
i'm familiar with inheritance and the general rules
it's just this one example
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
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