Re: Accessibility of suBclass-fields from suPERclass (reflection)
On 16.08.2014 14:37, Chris Uppal wrote:
Robert Klemme wrote:
If I understand your issue correctly: this is not about security but
about design principles. Dependency always goes up the inheritance
chain - not downwards. Meaning, all subclasses have knowledge about
their superclasses but not vice versa.
Agreed, though I think "always" is a little strong. It is possible to have
clusters of co-designed classes where the superclass "knows about" it's
subclasses. For instance the superclass could act as a factory for instances
of its sublclasses, where the subclasses (what they are and how many of them
there are, etc) is hidden from the consumer code.
Still then the superclass needs to know zero about subclass internals.
Of course, as long as the subclasses are visible to the superclass it
can use them like any other class (e.g. a separate factory class) could.
But that's a rather weak dependency and has nothing to do with
specifics of the interface or implementation of subclasses.
(In such cases I'd often like to be able to declare the superclass as "final
except that it can have subclasses in the same package")
I don't see the point. Actually it's contradicting: a final class does
not have subclasses - nowhere. Eric provided a good means how to
prevent subclassing in other places of the code where you do not want to
allow it.
Cheers
robert