Encapsulation and Data Hiding in Java

Tutorial 4 of 5

Encapsulation and Data Hiding in Java

1. Introduction

  • Goal of the tutorial: This tutorial aims to educate learners on the principle of encapsulation in Java, which is a crucial part of the OOP (Object-Oriented Programming) concepts. We will learn how to use encapsulation to safeguard our data and to control how it can be accessed and modified.
  • What the user will learn: By the end of this tutorial, users will understand encapsulation's meaning, its importance, and how to implement it in Java programs. We will cover encapsulation, private and public keywords, getters and setters, and demonstrate how to hide data within a class.
  • Prerequisites: Basic knowledge of Java programming, understanding of OOP concepts and Java classes will be beneficial.

2. Step-by-Step Guide

  • Encapsulation: Encapsulation is one of the four fundamental OOP concepts. It refers to the bundling of data with the methods that operate on that data. It is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them.
  • Private and Public Keywords: In Java, encapsulation can be achieved using access modifiers - private, public, and protected. The private keyword is the main actor for encapsulation. It makes the class attributes inaccessible directly from outside the class. The public keyword allows the class attributes to be accessed from outside the class.
  • Getters and Setters: These are the methods used to view and modify the private variables in a class. The getter method returns the variable value, and the setter method sets the value.

3. Code Examples

Example 1: A simple class without encapsulation

public class Employee {
  String name;
  int age;
}

In this example, the class attributes (name and age) can be accessed directly, which is not a good practice.

Example 2: A class with encapsulation

public class Employee {
  private String name;
  private int age;

  //Getter for name
  public String getName() {
    return name;
  }

  //Setter for name
  public void setName(String newName) {
    this.name = newName;
  }

  //Getter for age
  public int getAge() {
    return age;
  }

  //Setter for age
  public void setAge(int newAge) {
    this.age = newAge;
  }
}

In this example, the class attributes (name and age) are declared as private. They can't be accessed directly from outside the class. We have provided public getter and setter methods to access and modify these variables.

4. Summary

  • Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
  • Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
  • It provides control over the data and helps in achieving data hiding.

Next steps for learning:
- Explore other OOP concepts like Inheritance, Polymorphism, and Abstraction.
- Practice writing encapsulated classes and objects in Java.

Additional resources:
- Oracle Java Documentation
- Java Encapsulation (GeeksforGeeks)

5. Practice Exercises

Exercise 1: Create a Person class with private attributes: name and age. Then provide public getters and setters to manipulate these variables. Create a Person object in a main method and demonstrate getting and setting the attributes.

Exercise 2: Create a BankAccount class with private attributes: accountNumber and balance. Provide public methods for deposit, withdraw, and checkBalance. Make sure balance cannot go negative during withdrawal.

Tips for further practice:
- Try to create more complex classes with more attributes.
- Practice creating classes where some attributes are read-only (only provide getters) and some are write-only (only provide setters).
- Try encapsulating behavior as well (methods) in addition to the data (variables).