If a class follows data hiding and abstraction such type of class is said to be ‘Encapsulated‘ class.

Encapsulation = Data Hiding + Abstraction.

Example:

public class Account {
     
    private double balance;
 
    public double getBalance() {
        return balance;
    }
 
    public void setBalance(double balance) {
        this.balance = balance;
    }
}
  1. Hiding the data behind the methods is the central concept of encapsulation.i.e. We have to declare data members as private and we can provide access to that data by using getter and setter methods.
  2. The main advantages of encapsulation are
    • We can achieve security.
    • Enhancement will become very easy.
    • It improves maintainability.
  3. The main limitation of encapsulation is ,It increases length of the code and slow down the execution.

Tightly Encapsulation

A class is said to be tightly encapsulated if and only if all the data members are declared as private

Example:1

class A{

 private int x=10;

 public int getX(){
   return x;
 }
 public void setX(int x){
   this.x=x;
 }
}

The above class is tightly encapsulated class.

Example:2

class A {
 private int x=10;
 public int y=20;
}

class A is Not tightly encapsulated class.

Example:3

class A{

private int x=10;
}

class B extends A{
 private int y=20;
}

class C extends A{
 int z=30;
}

Class A and B are tightly encapsulated classes and C is not Tightly Encapsulated.

Example:4

class A{

 int x=10;
}

class B extends A{
 private int y=20;
}

class C extends A{
 private int z=30;
}

Classes A,B and C are not tightly encapsulated classes.

Note:If the parent is not Tightly Encapsulated,then no child class is tightly encapsulated.