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.

56 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
  23. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  24. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading Python training in pune new articles. Keep up the good work!

    ReplyDelete
  25. thanks for your details it's very use for my works web design company in velachery

    ReplyDelete
  26. Aluminium Composite Panel or ACP Sheet is used for building exteriors, interior applications, and signage. They are durable, easy to maintain & cost-effective with different colour variants.

    ReplyDelete
  27. I learned World's Trending Technology from certified experts for free of cost.i Got job in decent Top MNC Company with handsome 14 LPA salary, i have learned the World's Trending Technology from Python training in pune experts who know advanced concepts which can helps to solve any type of Real time issues in the field of Python. Really worth trying instant approval blog commenting sites

    ReplyDelete
  28. Attend The Data Science Courses in Bangalore From ExcelR. Practical Data Science Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Courses in Bangalore.
    ExcelR Data Science Courses in Bangalore

    ReplyDelete
  29. Your articles really impressed for me,because of all information so nice.informatica training in bangalore

    ReplyDelete
  30. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.dotnet training in bangalore

    ReplyDelete
  31. I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.hadoop training in bangalore

    ReplyDelete
  32. Very useful and information content has been shared out here, Thanks for sharing it.Microsoft azure training in bangalore

    ReplyDelete
  33. This is really an awesome post, thanks for it. Keep adding more information to this.tableau training in bangalore

    ReplyDelete
  34. 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 knowledge. Amazon web services Training in Bangalore

    ReplyDelete
  35. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.devops training

    ReplyDelete

  36. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. sap s4 hana training in bangalore

    ReplyDelete
  37. Awesome,Thank you so much for sharing such an awesome blog. sap fico training in bangalore

    ReplyDelete
  38. Thanks for sharing this blog. This very important and informative blog. Python Training in Bangalore

    ReplyDelete
  39. thank you so much for this nice information Article, Digitahanks for sharing your post with us.sap scm training in bangalore

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

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

    ReplyDelete
  42. 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
  43. really thanks for posting such an informative stuff...

    learn data science

    ReplyDelete