• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation in Java

 
Raveendran Hs
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way define a Encapsulation in java.
Can you please give the good example for this
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Encapsulation we making the fields in a class private and providing access to the fields via public methods.



In this code you can not access variables name , id age from outside the class directly . To access them you have to go through getter and setter methods. And here if you want then you can add some logic which will protect your variables.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raveendran, I again ask you to SearchFirst. Like your previous question, this question has been asked and answered several times before.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic