loops - Sub class instance variable in super-class object? -
let's have 2 classes called "superclass" , subclass. subclass extends superclass.
i found out it's not possible superclass have instance variable of type subclass.
that is, not work:
class superclass{ subclass x = new subclass(); }
because causes constructors call each other, entering perpetual loop. (because java automatically puts in call super())
is creating subclass instance variable impossible , bad design idea? or there kind of work-around?
it bad idea. if super class needs instance of subclass, that's code smell. creates circular dependencies , suggests broken analysis.
the workaround/fix bring whatever code super class using subclass super class.
Comments
Post a Comment