ENCAPSULATION

 Encapsulations in OOP


One of the fundamental principles of OOP, along with abstraction, polymorphism, and inheritance, is encapsulation. In basic terms, it refers to combining methods or functions that work with data which is typically referred to as fields with data itself into a single entity, typically a class. By limiting direct access to specific components of an entity, the encapsulation process guards against malicious interference and data exploitation.

Key Concepts of Encapsulation:-

Data Hiding:                                                                                                                                                  By granting restricted access to an object's data, encapsulation safeguards its internal state. The data of that object can only be changed using authorized methods.

Modifiers of Access:
Access modifiers are used to achieve the following encapsulation:

  • Private: "only within the class" is what the private modifier denotes.
  • Public: Anywhere in the program is what the public modifier denotes.
  • Default : Similar to Java, accessible only within classes that are part of the same package.
  • Protected: The protected modifier means within the class and subclass.
Getter and Setter Methods:                                                                                                                        To read and modify the values of private fields indirectly, encapsulation frequently uses getter and setter methods.
  • Getter: Gives back a private field's value.
  • Setters: may use validation logic to set a private field's value.

Benefits of Encapsulation:-

Data Security: Encapsulation makes data secure by preventing direct access to it.

Better Maintenance: As long as the enclosing class's interface remains constant, internal modifications to its data structures or functions can be done without relying on external code.

Increased Flexibility:
Because it offers validation or transformation logic as needed, it allows you to have control over how data is accessed and modified.

Improved Code Reusability: Encapsulation makes things more self-contained, which makes them much simpler to use, test, and maintain on their own.


Example :-

public class Person {

    // Private fields

    private String name;

    private int age;


    // Constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    // Getter for name

    public String getName() {

        return name;

    }


    // Setter for name

    public void setName(String name) {

        this.name = name;

    }


    // Getter for age

    public int getAge() {

        return age;

    }


    // Setter for age with validation

    public void setAge(int age) {

        if (age > 0) {

            this.age = age;

        } else {

            System.out.println("Age must be positive!");

        }

    }

}

public class Main {
    public static void main(String[] args) {
        Person person = new Person("Alice", 25);
        System.out.println(person.getName()); 
        person.setAge(5); 
        person.setAge(30);
        System.out.println(person.getAge()); 
    }
}






Comments

Popular Posts