File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Encapsulation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encapsulation in Java" Watch "Encapsulation in Java" New topic

Encapsulation in Java

Hs Raveendran

Joined: Feb 05, 2010
Posts: 23

What is the best way define a Encapsulation in java.
Can you please give the good example for this
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

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.

Life is easy because we write the source code.....
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

Raveendran, I again ask you to SearchFirst. Like your previous question, this question has been asked and answered several times before.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Encapsulation in Java