I am building a generic Tree
class, which supports inheritance of sub-trees. But I\'ve encountered some problems. Would you please kindly help me?
As I see it, there is no perfect solution to this problem. This is basically due to type erasure. The Erasure of Generic Methods article explains that your addChild(final Tree extends Leaf> subTree)
function will become a addChild(final Tree subTree)
function. So, even if you could somehow have a generic parameter
(not valid syntax!) it would be erased to addChild(final Tree subTree)
at compile time. Adding your runtime test will work though, so the edit you made will do the job.