3

I have a superclass called Seat (for a concert hall).

GoldSeat , SilverSeat and BronzeSeat are its subclasses.

I've always read to keep data private so as to enable encapsulation.

If I need to write methods that use these instance variables in the subclasses, is it considered acceptable to make them protected? Most of the similar stack overflow questions don't address correct object-oriented design, but rather focus on differences between access modifiers and the processing efficiency or technical difference of each. If I missed one, I apologise in advance and will happily review it.

I could use getter methods in the subclasses to get the instance variables, but that seems bizarre in this case, but at least the data would be private.

E. Rowlands
  • 343
  • 5
  • 16

2 Answers2

1

It is not 'bizarre' to call your own accessor methods. It actually makes a lot of sense. If your accessor methods contain code beyond simple set/get them you definitely want to call them. Perhaps rethink your aversion to 'pure' OO. And, of course, it maintains encapsulation.

Joe
  • 3,337
  • 1
  • 14
  • 11
  • OK, some things to definitely think about. And the accessor methods only return the instance variable values, nothing more. edit* it just seemed bizarre because my subclasses inherit the instance variables, but to manipulate them a superclass accessor method is called - maybe its not bizarre (I'm a beginner). – E. Rowlands Nov 13 '16 at 03:24
  • 1
    I started with OO in Smalltalk, where everything is private. One could argue that inheritance is the weakest part of the OO model as it violates encapsulation - as you pointed out above - and is often not used correctly. For example, What different attributes do you keep about your different seats? If there aren't any genuine different attributes (not *values* attributes) then they are not truly subclasses. See Java Design by Peter Coad for a good treatment of inheritance vs composition. – Joe Nov 13 '16 at 15:48
  • 1
    An accessor method is not supposed to be a simple passthrough to the instance var- or what would be the point? Accessor methods may do some processing in the setter - applying rules and validation or conversion, and one provides potentially *many* different getters; ex: getAsString, getAsXML for strings, or getAsFloat, getAsDouble, getAsInt for numbers. One of the core bits about encapsulation, is that the form of the instance var is hidden...encapsulated - so the get/set does not expose this in any way. By calling the get/set of the instance vars in the subclasses, you are protected. – Joe Nov 13 '16 at 15:49
  • Very, very interesting insight. Thank you! I'm going to have a chat with my lecturer today and ask what he prefers for this assignment. At the very least, even though I'm a beginner, I may apply your take on the whole thing going forward, and do some more research/thinking on the idea. – E. Rowlands Nov 14 '16 at 08:55
1

is it considered acceptable to make them protected?

Yes. I don't think it will break the law of encapsulation when you use protected modifier. Allow only the subclass to access the instance, we still control what should be accessed by others and who can access the instance.

I could use getter methods in the subclasses to get the instance variables, but that seems bizarre in this case

In some cases, you want to do some preprocessing before others can access the instance and you can put the preprocessing in the Getter.

Eugene
  • 10,627
  • 5
  • 49
  • 67
  • What I needed to know. I see your point about if using protected modifier, then responsibility will lie with us about writing code that defines who can access data and instances. So, with that responsibility in mind, it's ok that the entire package's classes can access them? – E. Rowlands Nov 13 '16 at 03:33
  • 2
    May this link will help to explain it. http://stackoverflow.com/questions/902922/why-does-the-protected-modifier-in-java-allow-access-to-other-classes-in-same – Eugene Nov 13 '16 at 03:40
  • Edit* - That gave some good explanations as to why protected allows package access. Thanks – E. Rowlands Nov 13 '16 at 03:45
  • 1
    Actually, it can, easily. http://stackoverflow.com/questions/9344935/inheritance-breaking-encapsulation – Joe Nov 13 '16 at 15:46
  • 1
    But, if you change the instance var name or implementation in the superclass then it can break in the subclasses. And this is one reason why we call our own getter and setters, even in subclasses. – Joe Nov 13 '16 at 15:51