Friday, September 2, 2016

JAVA 14 - Encapsulation

So far we've been playing around our first proper class that mimics a dice.

In this dice class I've told you the important of having our attributes as private, and using public methods to manipulate this data if we absolutely need to.

The problem is, we're testers, and we're not known for following rules particularly well ... so here it is ...

Make them public!  What's the worst that could happen?

Remember when Simba was told everything the light touches was his kingdom.  Except that shadowy place ... never go there?

Yeah - this ended up happening.  Damn you Simba, you had one job!

So here's our revised class - I've streamlined it.  And added those public attributes you asked for.

Cool - so let's run a test for it now!

I'm going to side with Uncle Scar on this ..."Simba what have you done?".

Here's the thing about private attributes.  They can only be accessed and changed by class methods.  If the attribute is vital, you can build recording around them to a log file to register changes.  In our class above, I've put print commands for all the methods.  If I had odd data, then I should check those methods with a fine tooth comb.

Now, here's the thing about public attributes.  They can be accessed and changed anywhere.  And on a large coding project, that's a frightening prospect.

It has the potential to be this ...

And sure, you have tools in your compiler and debugger which give you the equivalent of a metal detector and a powerful magnet.  But you've still made more work for yourself than you need.

Most people who write code end up feeling like this ...

Whilst you're learning is a great place to go "I wonder why they say I need to".  Learning is a great time to break rules, but also to learn some good habits.

This approach of only making public the methods that you really need to is called encapsulation, or data hiding.  A bit like the ring of power in Lord Of The Rings, ask yourself "Is it secret?  Is it safe?".

The big reveal

Yeah - well someone was nasty in the @Test method ...

The thing is, with the way you coded your DiceClass method, you didn't stop him from doing this!

You might notice how I'm blaming you dear reader for this, though I wrote it?  Let me just finish with this ...

That code that you made me write can be found here.

Extension material

Google some articles and read up about encapsulation.


  1. I have read your blog its very attractive and impressive. I like it your blog.
    Java Training institute in velachery

  2. I read encapsulation for java 14 whether it could be applied to the web version?

  3. Nicely explained. Here you described the well written article from your in-depth knowledge. Truly impressive and nice information

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

  4. perfect explanation about java programming .its very useful.thanks for your valuable information. java training in chennai | java training in velachery

  5. Very nice Information for Java languages, very nice informative and much effective as well.
    Java Training in Chennai