Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encapsulation in Java" Watch "Encapsulation in Java" New topic
Author

Encapsulation in Java

Hs Raveendran
Greenhorn

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: 629

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
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Encapsulation in Java
 
Similar Threads
can we achieve data hiding withe the help of encapsulation...
encapsulation and polymorphism
WA #1.....word association
How do we implement encapsulation and abstarction in java programming wise?
Abstraction