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.

31 comments:

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

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

    ReplyDelete
  3. very good information.it is very useful for java learners.keep rocks.thanks fpr sharing.

    Best Java Training Institute in Chennai

    ReplyDelete
  4. Very useful and information content has been shared out here, Thanks for sharing it.
    Visit Learn Digital Academy for more information on Digital marketing course in Bangalore.

    ReplyDelete
  5. nice blog . check out https://ayevainstitute.com/

    ReplyDelete

  6. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    aws training in bangalore
    RPA Training in bangalore
    Python Training in bangalore
    Selenium Training in bangalore
    Hadoop Training in bangalore

    ReplyDelete
  7. For Python training in Bangalore, Visit:- Python training in Bangalore

    ReplyDelete
  8. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful.sap crm training in bangalore

    ReplyDelete
  9. I have read your blog its very attractive and impressive. I like it your blog.sap ewm training in bangalore

    ReplyDelete
  10. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledgez.

    ReplyDelete
  11. really thanks for posting such an informative stuff...

    learn data science

    ReplyDelete
  12. Attend The Course in Data Analytics From ExcelR. Practical Course in Data Analytics Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Course in Data Analytics.
    ExcelR Course in Data Analytics
    Data Science Interview Questions

    ReplyDelete
  13. Your article is very informative. It's a welcome change from other supposed informational content. Your points are unique and original in my opinion. I agree with many of your points.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  14. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Best Training Institute for AWS in Pune

    ReplyDelete
  15. Excellent blog,Keep sharing such type of post..

    Full stack classes in ahmedabad

    ReplyDelete