Take a look in topics related to Field Shadowing.
In brief, there is no "overriding" for member variable in Java. If you declare a member variable with the same name in your child class as your parent, the child member variable is "shadowing" the parent's.
Use your code as an example, in a LeafNode
instance, there are actually two member variables called bounds, which you can think of as "Node::bounds" and "LeafNode::bounds".
if you are accessing through a Node
reference, you are going to interact with Node::bounds
, and LeafNode::bounds
if through an NodeLeaf
reference.
e.g.
LeafNode leafNode = new LeafNode();
Node node = leafNode;
leafNode.bounds = xxx; // accessing LeafNode::bounds
node.bounds = yyy; // accessing Node::bounds
In your case, I don't see any reason for keeping a separate bounds
in child class. Consider changing Node::bounds
to be protected and use that in your child class instead.