Flat Preloader Icon

Encapsulation And Access Control

Encapsulation and Access Control

  • Encapsulation
  • Access Control Modifiers
    • Public
    • Private
    • Protected
    • Default

The this Keyword

  • We use “this” keyword from any method or constructor to refer to the current object
  • Used to distinguish class level variables from local variables
  • E.g. this.color, this.area etc

Encapsulation

  • Encapsulation is one of the four fundamental OOP concepts, along with inheritance, polymorphism, and abstraction.
  • It refers to the practice of bundling data (fields) and methods (functions) that operate on that data into a single unit called a class.
  • Encapsulation restricts direct access to some of an object’s components and protects the integrity of the data by preventing unintended modifications.
  • It promotes the idea of “hiding” the internal details of an object from the outside world, providing controlled and well-defined interfaces for interacting with the object.
  • To achieve encapsulation in Java:
    • Declare class fields as private to hide them from direct access outside the class.
    • Provide public methods (getters and setters) to allow controlled access to the private fields. These methods are often referred to as accessors and mutators.
  • Here’s an example illustrating encapsulation in Java:
				
					public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String newName)
    {
        if (newName != null && !newName
        .isEmpty())
        {
            name = newName;
        }
    }

    public int getAge() 
    {
        return age;
    }

    public void setAge(int newAge)
    {
        if (newAge >= 0) {
            age = newAge;
        }
    }
}

				
			

This Keyword

  • We use “this” keyword from any method or constructor to refer to the current object
  • Used to distinguish class level variables from local variables
  • E.g. this.color, this.area etc
In this example, the Student class encapsulates the name and age fields and provides getter and setter methods to control access to these fields.

Access Control

  • Access control refers to the rules and mechanisms that determine which parts of a class (fields, methods, classes, and packages) are visible and accessible from different parts of a program.
  • Java provides four access modifiers for controlling access to class members:
    • public: Members are accessible from anywhere.
    • private: Members are accessible only within the same class.
    • protected: Members are accessible within the same package and subclasses (even if they are in different packages).
    • default (package-private): Members are accessible within the same package.

How Encapsulation and Access Control Work Together in Java

  • To achieve encapsulation, you typically declare the fields of a class as private, so they can only be accessed directly within that class.
  • You provide public methods (getters and setters) to control access to these fields. These methods allow you to enforce rules, constraints, or validation when reading or modifying the data.
  • Here’s an example:
				
					public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name != null && !name
        .isEmpty()) {
            this.name = name;
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 0 && age <= 120) {
            this.age = age;
        }
    }
}

				
			
In this example, the name and age fields are encapsulated and declared as private. Public getter and setter methods are provided to control access to these fields. The setter methods enforce rules (non-null and non-empty for name, and age within a reasonable range for age) before modifying the data.