ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Encapsulation

Encapsulation

In Java data encapsulation is concerned with:
Combining data and how it’s manipulated in one place.
Only allowing the state of an object to be accessed and modified through behaviors.
Hiding the details of how the object works.


Example:

  1. public class Vegetable
  2. {
  3.  
  4. private String name;
  5. private String price;
  6. private String taste;
  7.  
  8. private Vegetable(String name, String price, String taste)
  9. {
  10. this.name = name;
  11. this.price = price;
  12. this.taste = taste;
  13. }
  14.  
  15. public void Cost()
  16. {
  17.  
  18. }
  19.  
  20. public String getName()
  21. {
  22. return name;
  23. }
  24.  
  25. public void setName(String name)
  26. {
  27. this.name = name;
  28. }
  29.  
  30. public String getPrice() {
  31. return price;
  32. }
  33.  
  34. public void setPrice(String price) {
  35. this.price = price;
  36. }
  37.  
  38. public String getTaste() {
  39. return taste;
  40. }
  41.  
  42. public void setTaste(String taste) {
  43. this.taste = taste;
  44. }
  45.  
  46. }
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!