java - For polymorphic classes, is it OK to use protected instance variables? -
i have superclass called seat (for concert hall).
goldseat , silverseat , bronzeseat subclasses.
i've read keep data private enable encapsulation.
if need write methods use these instance variables in subclasses, considered acceptable make them protected? of similar stack overflow questions don't address correct object-oriented design, rather focus on differences between access modifiers , processing efficiency or technical difference of each. if missed one, apologise in advance , happily review it.
i use getter methods in subclasses instance variables, seems bizarre in case, @ least data private.
is considered acceptable make them protected?
yes. don't think break law of encapsulation when use protected modifier. allow subclass access instance, still control should accessed others , can access instance.
i use getter methods in subclasses instance variables, seems bizarre in case
in cases, want preprocessing before others can access instance , can put preprocessing in getter.
Comments
Post a Comment