I have a class TreeNode
, a class LeafNode
which extends TreeNode
and a class ParentNode
that extends TreeNode
as well. In the class ParentNode
I have a method getChild
that returns a TreeNode
.
Whenever I call getChild
successively, I have to do a bunch of downcasting.
LeafNode myLeafNode = (LeafNode) ((ParentNode) this.getChild(id1)).getChild(id2)
Is there a way I can avoid all this downcasting without changing the inheritance structure of my program?