A Beginners Guides to Encapsulation in Java with Real Time Example

Introduction to Java Encapsulation

Encapsulation = Data Hiding + Abstraction

It simply means that any component is following abstraction and data hiding then it is also following Encapsulation.

The Process of wrapping data members and abstraction together in a unit, it is known as Java Encapsulation. In Java, Each and every class is an example of Encapsulation. In other words, it's just like capsule medicine.

f:id:javatpoint:20181222214946p:plain

Encapsulation in Java with Example

Data hiding – Data hiding is used to keeping our internal data safe from unauthorized access. No one can access our internal data. By declaring data members as private, we can achieve Data Hiding. It means the third person can use our data through a public method which will validate the user by asking id and password.

So, by applying the concept of data hiding, we will achieve Security.

Abstraction Abstraction is the mechanism to hiding the unnecessary things from the users and provide only functionality.

Abstract Class -

Through "abstract" keyword, we can declare a class is abstract.

  1. Abstract classes may or may not carry abstract methods,

i.e., methods without the body -

                        public void get();

  1. if a class has at least one abstract method, then the class must be declared abstract.
  2. Abstract class cannot be instantiated.
  3. You must inherit an abstract class from another class, and provide implementation.

Abstract Method- if you want the actual implementation of that methods which is determined by child class, you must declare a class is abstract in parent class.

  1. you must place the "abstract" keyword before the method name in the method declaration.
  2. An abstract method includes a method signature, but not method body.
  3. Instead of curly braces, at the end of abstract method there is a semicolon (;)

Why we Use Encapsulation?

There are 3 core features of encapsulation for using it. These are –

  1. Flexibility – It is more flexible to change the code according to requirements. i.e. if we want to change the age of person so we can quickly change the logic in the set() method.
  1. Reusability – we can reuse our code in our application and across the multiple applications.
  1. Maintainability – Programme code is encapsulated in separate parts (classes, interfaces, methods, setters, getters, etc.) so it’s easy to change a part of the application without moving other elements, which decreases the time of maintenance.

How to achieve Encapsulation ?        

  1. To achieve encapsulation, provide getter( ) and setter( ) method in the class to set the variables.
  2. Declaring private to the instance variables of the class.

 

Getter and setter methods –

In java, setter method is used to set the value of variables. And also known as mutator method.

Getter method is used to return the value of private member of the class. and also known as accessor method.

Let’s take an example to better understand –

 

public class Company {

private String name;

private String email;

private int salary;

public Company(String name, String email, int salary){

    this.name = name;

this.email = email;

this.salary = salary;

}

public void setName(String name){

    this.name = name;

}

public void setEmail(String email){

this.email = email;

}

public void setSalary(int salary){

this.salary = salary;

}

public String getName(){

    return this.name;

}

public String getEmail(){

    return this.email;

}

public int getSalary(){

   return this.salary;

}

public static void main(String[] args)

{

    Company me = new Company("Anjali","anjali@gmail.com",2000);

    System.out.println("My name is:" + me.getName());

me.setEmail("anjalirajput@gmail.com");

    System.out.println("My email is:" + me.getEmail());

}

}

Output:

My name is :Anjali

My email is:anjalirajput@gmail.com

 

Advantage of Java Encapsulation

  1. It is Flexible so we can quickly update code with our requirements.
  2. We can change the value of variables by using getter and setter methods.
  3. If you don’t define setter method than the field can be read-only.
  4. If you don’t define getter method than the field can be write-only.
  5. Other advantage is that we can reuse our code across the multiple applications.
  6. Without moving other elements, we can do changes there. 

Java Encapsulation Real-Time Example:

Let’s suppose you go to a mall and there is one automatic cold drink machine. So, you requested for a cold drink and  the machine implement your request and gives you cold drink. Here, machine is a class. It carry data(Cold drinks) and operations(Service) both. And wrapping it into one unit that is machine. This is called Encapsulation.

And how machine is working you doesn’t  need to know, this is called Abstraction. And you get cold drink after request so how many cans it contain and mechanism , service you cannot access all these operations, this is called Data Hiding.