SA

OOP Concepts - Part 1 - Encapsulation_Aggregation

Object-oriented Programming Features

Part 1: Encapsulation and Aggregation


Encapsulation

  • Definition: A method to hide data, properties, and methods from outside access and only expose necessary components.

  • Functionality: Acts as a protective barrier preventing random access to code and data from outside the class.

  • Analogy: Similar to a capsule that contains various medicines, encapsulation wraps code and data into a single unit.


Data Hiding

  • User Experience: Users are unaware of the inner workings of a class. The implementation details are hidden from them.

  • Setter and Getter Methods: Users only interact with the class through setter methods to pass values, which get initialized into variables.

  • Benefits:

    • Increased Flexibility: Variables can be made read-only or write-only based on requirements.

    • Reusability: Improved reusability; easier to adapt for new requirements.

    • Testing: Easier to conduct unit testing on encapsulated code.


Access Modifiers

  • Public: Accessible everywhere, has the widest access scope among modifiers.

  • Protected: Accessible within the package and outside only through inheritance.

  • Private: Accessible only within the class itself.

  • Default: If no modifier is used, it’s only accessible within the package (default access).


Access Modifier Accessibility Summary

Owning Class

Child Class

Other Class

Other Package

Public

Yes

Yes

Yes

Yes

Protected

Yes

Yes

No

No

Private

Yes

No

No

No

Default

Yes

Yes

No

No

/


Implementing Encapsulation in Java

  • Steps:

    • Declare class variables as private.

    • Provide public setter and getter methods to modify and view the variables' values.


Aggregation

  • Definition: A special form of association that is unidirectional, also referred to as a "has-a" relationship.

  • Purpose: Enables an object to be composed of other objects, forming a whole.

  • Example: A person consists of a head object, a body object, and two hand objects, among others.


Aggregation in Code

public class Person {
    public Name name;

    public Person(Name name) {
        this.name = name;
    }
}

public class Name {
    public String firstname;
    public String lastname;

    public Name(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
}

public class Test {
    public static void main(String[] args) {
        Person person = new Person(new Name("John", "Snow"));
        System.out.println("Hello, " + person.name.firstname);
    }
}

Getter and Setter Methods Getter and setter methods are used to access and modify private variables in a class.

  • Getter Method: A method that retrieves the value of a private variable. It provides read access to the variable.

  • Setter Method: A method that allows for setting or updating the value of a private variable. It provides write access to the variable.

Functionality:

  • They help maintain encapsulation by restricting access to the internal state of the object and only allowing it through controlled methods.

  • Users can’t directly modify the variables, which helps in validating input data before updating.

Example:

public class Person {  
   private String name;  
   
   // Getter method  
   public String getName() {  
       return name;  
   }  
   
   // Setter method  
   public void setName(String name) {  
       this.name = name;  
   }  
}  

In this example, getName() allows users to access the name variable, while setName(String name) allows users to modify it.