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

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -