In the Class A or Class B all the fields are public. So you can access them using <instanceName>.<fieldName>. In the example I provided I had made the fields are private and then provided Getters/Setters to access/manipulate the fields.
I just did not want to post up my entire actual code but I will post my actual code and go from there.
All of that was given as the teacher's code - he has said not to change it because it should work fine and the only changes we should make is adding our code where he told us to add it (as noted with the comments)
I am expected to use a stack to bind variables, but before I can do so, I need to figure out how to get to those variables. For example, in main, Expr e = new Block(new . . . . . );
e is then passed as a parameter
What can I do within the eval method to get to the variables that were instantiated in e ("x", new Declaration("x"........) etc.?
It is of course perfectly okay if you cannot help because I definitely appreciate you trying to understand me (I can be confusing) and understand what I am trying to do. This is much appreciated.
In the eval method-
Firstly you need to cast "e" to Block to be able to use Block class's instance fields. That way you would have to proceed to access other class's instance fields. And before casting try to add an instanceof check. The reason for casting is that- Which fields can be accessed is determined by the Type of the reference.
So if you have the type of reference as Expr and your type of instance is Block, to access the left field in the Block instance you need to have the Type of reference as "Block" and not "Expr". Because the association of the fields with the reference type is done at the compile time.