Encapsulation in Java: Theory, Implementation and Best Practices

Share your love

Index

What is Encapsulation in Java?

Data Hiding

Example Time !!!

Encapsulation in Java

Understanding Encapsulation in Java with the help of the code.

class Human {
  int age;
  String name;
}

public class Demo {
  public static void main (String args[]) {
    Human obj = new Human();
    obj.age = 54;
    obj.name = "Ateev";
    
    System.out.println(obj.name + " is " + obj.age + " years old");
  }
}

Output

class Human {
  private int age;
  private String name;
}

public class Demo {
  public static void main (String args[]) {
    Human obj = new Human();
    obj.age = 54;
    obj.name = "Ateev";
    
    System.out.println(obj.name + " is " + obj.age + " years old");
  }
}

Output

class Human {
  private int age;
  private String name;
  
  public int getAge() {
    return age;
  }
  
  public String getName() {
    return name;
  }
}

public class Demo {
  public static void main (String args[]) {
    Human obj = new Human();
    obj.age = 54;
    obj.name = "Ateev";
    
    System.out.println(obj.name + " is " + obj.age + " years old");
  }
}

Output

class Human {
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    
    public String getName() {
        return name;
    }
}

public class Demo {
    public static void main(String args[]) {
        Human obj = new Human();
        obj.age = 54;
        obj.name = "Ateev";
        System.out.print(obj.getName() + ": " + obj.getAge());
    }
}

Output

System.out.println(obj.name + " : " + obj.age);
 System.out.println(obj.getName() + " : " + obj.getAge());
class Human {
  private int age;
  private String name;
  
  public int getAge() {
    return age;
  }
  
  public String getName() {
    return name;
  }
}

public class Demo {
  public static void main (String args[]) {
    Human obj = new Human();
    System.out.println(obj.getName() + " is " + obj.getAge() + " years old");
  }
}

Output

Declaring and initialising the variables at the same time

class Human {
  private int age = 54;
  private String name = "Ateev";
  
  public int getAge() {
    return age;
  }
  
  public String getName() {
    return name;
  }
}

public class Demo {
  public static void main (String args[]) {
    Human obj = new Human();
    System.out.println(obj.getName() + " is " + obj.getAge() + " years old");
  }
}

Output

Declaring the variables in the Human class and initialising them in the Demo class.

class Human {
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int a) {
        age = a;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String n) {
        name = n;
    }
}

public class Demo {
    public static void main(String args[]) {
        Human obj = new Human();
        obj.setAge(54);
        obj.setName("Ateev");
        System.out.print(obj.getName() + " is " + obj.getAge() + " years old");
    }
}

Output

Getter and Setter Public Methods for Encapsulation in Java

class Human {
    private int age;
    private String name;
    
    public int AAA() {
        return age;
    }
    
    public void GGG(int a) {
        age = a;
    }
    
    public String BBB() {
        return name;
    }
    
    public void HHH(String n) {
        name = n;
    }
}

public class Demo {
    public static void main(String args[]) {
        Human obj = new Human();
        obj.GGG(54);
        obj.HHH("Ateev");
        System.out.print(obj.AAA() + " is " + obj.BBB() + " years old");
    }
}

Output

Why do we need Encapsulation in Java?

Data Protection

Controlled Access

Code Maintenance: 

Understanding Encapsulation in Java, the professional way

class Human {
    private int age;
    private String name;

    public int getAge() { return age; }
    public void setAge(int a) { age = a; }

    public String getName() { return name; }
    public void setName(String n) { name = n; }
}
class Human {
    private final String name;
    private int age;

    public Human(String name, int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a Human object
        Human person = new Human("Alice", 25);

        // Print initial details
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

Output

class Human {
    private String name;
    private int age;

    public Human(String name, int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void celebrateBirthday() {
        age++;
    }

    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a Human object
        Human person = new Human("Alice", 25);

        // Print initial details
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());

        // Celebrate birthday
        person.celebrateBirthday();

        // Print updated age
        System.out.println("After birthday, Age: " + person.getAge());
    }
}

Output

Industrial best practices for Encapsulation in Java

class Human {
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int a) {
        age = a;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String n) {
        name = n;
    }
}

public class Demo {
    public static void main(String args[]) {
        Human obj = new Human();
        obj.setAge(54);
        obj.setName("Ateev");
        System.out.print(obj.getName() + " is " + obj.getAge() + " years old");
    }
}

Use constructors to enforce a valid object state

public class Main {

    public static void main(String[] args) {
        Human person = new Human("Ateev", 54);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

class Human {
    private String name;
    private int age;

    public Human(String nameValue, int ageValue) {

        if (nameValue == null || nameValue.isEmpty()) {
            throw new IllegalArgumentException("Invalid name");
        }

        if (ageValue < 0) {
            throw new IllegalArgumentException("Invalid age");
        }

        name = nameValue;
        age = ageValue;
    }

    public String getName() { 
        return name; 
    }

    public int getAge() { 
        return age; 
    }
}

Output

Guarding Object Integrity

public class Main {

    public static void main(String[] args) {
        Human person = new Human("Ateev", 54);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

class Human {
    private String name;
    private int age;

    public Human(String nameValue, int ageValue) {

        if (nameValue == null || nameValue.isEmpty()) {
            throw new IllegalArgumentException("Invalid name");
        }

        if (ageValue < 0 || ageValue > 100) {
            throw new IllegalArgumentException("Invalid age");
        }

        name = nameValue;
        age = ageValue;
    }

    public String getName() { 
        return name; 
    }

    public int getAge() { 
        return age; 
    }

    public void setAge(int value) {
        if (value < 0 || value > 100) {
            throw new IllegalArgumentException("Invalid age");
        }

        age = value;
    }
}

Restrict Setter Visibility When needed

Use encapsulation to reduce coupling

class Human {

    private int age;
    private String name;

    public Human(String n, int a) {
        name = n;
        age = a;
    }

    public String introduce() {
        return name + " is " + age + " years old";
    }
}

public class Demo {

    public static void main(String[] args) {

        Human person = new Human("Ateev", 54);

        System.out.println(person.introduce());
    }
}

Output

Conclusion

Frequently Asked Question

Q1. What is the difference between encapsulation and abstraction?

Q2. What happens if we don’t use encapsulation in a Java program?

Q3. Is it possible to encapsulate a method in Java?

Q4. What are the design patterns that heavily rely on encapsulation?

Q5. Is it possible to overuse the Getter and Setter methods?

Share your love