- Java - Inner classes
- Java - Exceptions
- Java - Files and I/O
- Java - Methods
- Java - Regular Expressions
- Java - Date & Time
- Java - Arrays
- Java - Strings
- Java - Characters
- Java - Numbers
- Java - Decision Making
- Java - Loop Control
- Java - Basic Operators
- Java - Modifier Types
- Java - Variable Types
- Java - Basic Datatypes
- Java - Constructors
- Java - Object & Classes
- Java - Basic Syntax
- Java - Environment Setup
- Java - Overview
- Java - Home
Java Object Oriented
- Java - Packages
- Java - Interfaces
- Java - Encapsulation
- Java - Abstraction
- Java - Polymorphism
- Java - Overriding
- Java - Inheritance
Java Advanced
- Java - Documentation
- Java - Applet Basics
- Java - Multithreading
- Java - Sending Email
- Java - Networking
- Java - Serialization
- Java - Generics
- Java - Collections
- Java - Data Structures
Java Useful Resources
- Java - Examples
- Java - Discussion
- Java - Useful Resources
- Java - Quick Guide
- Java - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java - Encapsulation
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java −
Declare the variables of a class as private.
Provide pubpc setter and getter methods to modify and view the variables values.
Example
Following is an example that demonstrates how to achieve Encapsulation in Java −
/* File name : EncapTest.java */ pubpc class EncapTest { private String name; private String idNum; private int age; pubpc int getAge() { return age; } pubpc String getName() { return name; } pubpc String getIdNum() { return idNum; } pubpc void setAge( int newAge) { age = newAge; } pubpc void setName(String newName) { name = newName; } pubpc void setIdNum( String newId) { idNum = newId; } }
The pubpc setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any class that wants to access the variables should access them through these getters and setters.
The variables of the EncapTest class can be accessed using the following program −
/* File name : RunEncap.java */ pubpc class RunEncap { pubpc static void main(String args[]) { EncapTest encap = new EncapTest(); encap.setName("James"); encap.setAge(20); encap.setIdNum("12343ms"); System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge()); } }
This will produce the following result −
Output
Name : James Age : 20
Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.