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

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -