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.

28 comments:

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

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

    ReplyDelete
  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

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

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

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

    Best Java Training Institute in Chennai

    ReplyDelete
  7. Online casino games of Cambodia.
    IBCbet Is a casino web site. The famous club of Cambodia. One of the most popular online casino games providers has become popular and popular among gamblers around the world. Playing online gambling games in this way allows all players to play online gambling games with only the Internet and play at any time desired

    And important on the web page of the club. Online casinos also have a lot of online games to choose from, like playing casino games in general. Therefore, the online gambling games in each form is considered to be enjoyable and suitable for people who love and passionate about gambling. Therefore, playing online gambling is considered as an investment destination. little If players are thinking to play online gambling games, investment from our online casino, which is considered to be the best.

    Because of the online gambling games on the site Poipet. Our casino allows players to create new experiences. The satisfaction of the risky and can also join in the joy and play online gambling games that want to play today at the site Sbobet

    ReplyDelete
  8. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Are you looking for the best Java training institute in Chennai?
    Come and learn with Aorta, the best java training institute in Chennai offering the best platform to learn and get the depth of Java
    java training in chennai
    java course in chennai

    ReplyDelete
  9. Our digital marketing course in Chennai is targeted at those who are desirous of taking advantage of career opportunities in digital marketing. Marketing professionals who are presently using traditional marketing to meet their business objectives. Start-ups, Entrepreneurs, Business Owners who desire to make use of online media to improve their business goals and objectives.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai
    Online Digital Marketing Training
    SEO Training in Chennai

    ReplyDelete
  10. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  11. 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.
    python course in pune
    python course in chennai
    python course in Bangalore

    ReplyDelete
  12. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    excel advanced excel training in bangalore
    Devops Training in Chennai

    ReplyDelete
  13. 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
  14. nice blog . check out https://ayevainstitute.com/

    ReplyDelete
  15. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Interview Questions And Answers, Top 250+AWS Interviews Questions and Answers 2018
    Advanced AWS Interview questions and answers | Top 110 AWS Interview Question and Answers
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore

    ReplyDelete
  16. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Interview Questions And Answers, Top 250+AWS Interviews Questions and Answers 2018
    Advanced AWS Interview questions and answers | Top 110 AWS Interview Question and Answers
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore

    ReplyDelete
  17. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
    Best AWS Amazon Web Services Training in Chennai | Best AWS Training and Certification for Solution Architect in Chennai
    Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai

    ReplyDelete
  18. I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
    For more details: ReactJs training in Velachery | ReactJs training in chennai

    ReplyDelete
  19. This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay, 
    python Online training in chennai
    python Online training in bangalore
    python interview question and answers

    ReplyDelete
  20. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in chennai
    Best rpa training in bangalore
    rpa course in bangalore
    rpa training in marathahalli
    rpa training in btm
    best rpa training in chennai

    ReplyDelete
  21. Read all the information that i've given in above article. It'll give u the whole idea about it.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete

  22. 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