• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Code reviews

How in gods name does only using public methods break encapsulation?

You are just speaking complete mince now love.

Possibly he meant methods and data?
As in to make all methods and data public would break encapsulation.
 
For anything non-super trivial, the only way you can get away with only having public methods is to ignore encapsulation completely and just make all your would-be private methods public, OR you have to duplicate code all over the place.

One of those is just dumb, and the other would prompt me to ask why you were even using Java in the first place?

You can still have encapsulation with public methods, the caller of your public method doesn't know or care how you implement the method, as long as the signature never changes. Encapsulation to me more about the internal state of the object being hidden, so private variables etc.
 
Putting a public accessor/mutator in front of private data doesn't magically make it encapsulated either.

So making all private data public would break encapsulation? Isn't that what I said?
 
Last edited: 11:30
You can still have encapsulation with public methods, the caller of your public method doesn't know or care how you implement the method, as long as the signature never changes. Encapsulation to me more about the internal state of the object being hidden, so private variables etc.

Exactly - so for anything but the simplest class imaginable, if you don't want to duplicate code all over the place you will have smaller reusable methods. Those methods are not meant to be exposed to the outside world because they are for manipulating the internal state.
So if you make them public you are breaking encapsulation because the outside world is now not restricted to interact via the appropriate public api; now they can **** about with the internal state of that object unrestricted.
 
So he's of the "C with classes" school?
 

Advertisers

Back
Top