< Previous Next >
OOPS Tutorial
1. Overview
In this post, we will learn the important object-oriented concept Encapsulation.
Encapsulation refers to combining data and associated methods as a single unit. In this post, we will learn Encapsulation in detail with examples.
Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.
In OOP, data and methods operating on that data are combined together to form a single unit, which is referred to as a Class. Read more about Java Class here.
Advantage of Encapsulation
The main advantage of using encapsulation is to secure the data from other methods. For example, when we make a fields private then these fields only use within the class, but these data not accessible outside the class.Real-world examples
- Capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside a capsule.
- A Java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
3. Implementation
The process of binding or wrapping the data and the codes that operate on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
For example - if a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class.
Consider below Person class diagram, the id and name parameters should not be accessed directly outside Person class - achieved by private declaration.
Let's create a Person class to demonstrates the use of encapsulation in Java.
Step 1: Create a Person class.
Step 1: Create a Person class.
public class Person {
private double id;
private String name;
public Person() {
// Only Person class can access and assign
id = Math.random();
sayHello();
}
// This method is protected for giving access within Person class only
private void sayHello() {
System.out.println("Hello - " + getId());
}
public double getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 2: Let's test the Encapsulation via a main() method.
public class EncapsulationDemostration {
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("Ramesh");
/*
* Note: As id and name are encapsulated in Person class, those cannot be accessed
* directly here. Also there is no way to assign id in this
* class. Try to uncomment below code and you would find compile time error.
*/
// p1.id = "123";
// p1.name = "this will give compile time error";
// p1.sayHello(); // You can't access this method, as it is private to Person
System.out.println("Person 1 - " + p1.getId() + " : " + p1.getName());
}
}
4. Difference between Abstraction and Encapsulation
Abstraction and Encapsulation in Java are two important Object-oriented programming concept and they are completely different from each other.
- Encapsulation is a process of binding or wrapping the data and the codes that operate on the data into a single entity. This keeps the data safe from outside interface and misuse.
- Abstraction is the concept of hiding irrelevant details. In other words, make the complex system simple by hiding the unnecessary detail from the user.
- Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifiers.
- Abstraction solves the problem at the design level. Whereas Encapsulation solves the problem at the implementation level.
Encapsulation: Information hiding.
Abstraction: Implementation hiding.
GitHub Repository
The source code of this article available on my GitHub repository at OOPS Concepts in Java.Learn complete Java Programming with Examples
Related Oops Posts
Top Core Java Tutorials
- Java Tutorial for Beginners
- 50 Java Keywords
- JDBC 4.2 Tutorial
- All Java/J2EE Tutorial
- Java 8 Tutorial
- Java Collections Tutorial
- Java Exceptions Tutorial
- Java Generics Tutorial
- Java 8 Stream API Tutorial
- Java Wrapper Classes
- Java Arrays Guide
- Java Multithreading Tutorial
- Java Concurrency Tutorial
- Oops Concepts Tutorial
- Java String API Guide
- Java Reflection API Tutorial
- Java I/O Tutorial
- Date and Time API Tutorial
- JUnit 5 Tutorial
- JUnit 4 Tutorial
- Java XML Tutorial
- Google GSON Tutorial
Top Java EE Tutorials
- Spring Security Tutorial
- RabbitMQ Tutorial
- Hibernate ORM 5
- Java Persistence API
- Spring Boot 2 Tutorial
- Spring Core 5 Tutorial
- Spring MVC 5 Tutorial
- Spring Data JPA Tutorial
- Apache HttpClient Tutorial
- Spring Framework 5
- Apache Maven Tutorial
- JAX-RS Tutorial
- Jersey Rest Tutorial
Java Library Tutorials
- Java API Guides
- Java SQL Package Tutorial
- All Java/J2EE Tutorial
- Java Lang Package Tutorial
- Java Util Package Tutorial
- Java Lang Reflect Package Tutorial
- Java Time Package Tutorial
- Java IO Package Tutorial
Top Java Best Practices
- Java Enums and Annotations Best Practices
- Java Generics Best Practices
- JUnit Framework Best Practices
- Java Naming Conventions
- Single Responsibility Principle
- Liskov's Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
- Open Closed Principle
- Oops principles in java
- Restful API Best Practices
- JSP Best Practices
- Guide to JDBC Best Practices
- Collection Best Practices
- String Best Practices in Java
- Exception Handling Best Practices
- Synchronization Best Practices
- Guide to JDBC Best Practices
- Serialization Best Practices
Thanks for publishing this blog, really awesome. Its help me to clarify my doubts well.
ReplyDeleteC C++ Training in Chennai
C Training in Chennai
C++ Training in Chennai
JMeter Training in Chennai
JMeter Training Institute in Chennai
Appium Training in Chennai
javascript training in chennai
core java training in chennai