1. Understanding Data Hiding

Data hiding, as the term suggests, involves concealing internal data from external access. The internal data, often deemed sensitive and private, should not be directly accessible by external entities. Ensuring the privacy and security of data is a fundamental aspect of software design.

1.1 Example - Implementing Data Hiding in Programming

Let's illustrate data hiding through a programming example. Consider a class named Account, representing a simple application where we want to retrieve our account balance. To achieve this securely, we create a method called getBalance().

class Account {
  private double balance;

  public double getBalance() {
    // Validation
    if (valid) {
      return balance;
    }
  }
}

In this example, the balance variable is marked as private, indicating that it should not be directly accessible. The getBalance() method includes a validation check to ensure that only authorized users can access the balance information.

By implementing data hiding, we enhance the security of our application. The main advantage lies in preventing unauthorized access to sensitive data. It is a best practice to declare data members or variables as private to promote data hiding and, consequently, better security.

2. Understanding Abstraction

Abstraction is a concept that involves presenting only the essential features of an object or system while concealing the intricate details of its internal workings. This technique allows developers to focus on the functionality provided to users, emphasizing what the system does rather than how it achieves it.

2.1 Abstraction in Real Life

To grasp the idea of abstraction, consider the concept in real life. Abstraction, in general, refers to something existing in the mind but lacking physical presence—a notion that is incomplete and partially implemented. In programming, this translates to showcasing setup services to clients while keeping the internal implementation hidden. The setup service presented to external parties is termed abstraction.

For instance, when using an ATM to withdraw money, the user interacts with an interface displayed on the screen. The interface exposes various functions, such as 'withdraw.' The details of how this function accesses the user's balance and displays it are concealed within the system's internal implementation, which may be developed using a programming language like Java.

Should the need arise to enhance features beyond what Java supports, such as incorporating .NET, the interface remains unchanged. Abstraction, therefore, revolves around hiding internal intricacies while showcasing the offered setup services.

2.2 Advantages of Abstraction

Abstraction offers several advantages:

  1. Enhanced Security: By hiding internal details, abstraction contributes to system security.
  2. Ease of Enhancement: The focus on the interface simplifies the process of adding or improving features without altering the external presentation.
  3. Maintainability: Concealing complexities enhances the system's maintainability by isolating changes to the internal implementation.
  4. Modularity: Abstraction promotes modularity, allowing developers to work on independent components without affecting the entire system.

3. Understanding Encapsulation

Encapsulation, derived from the word 'CAPSULE,' involves grouping data members and behavior into a single unit, akin to creating a capsule in medicine that encapsulates various substances. In the realm of programming, encapsulation in Java is the process of integrating data (variables) and the code that operates on them (methods) within a class, forming a cohesive unit. By encapsulating a class's variables, access is restricted to the class's methods only, enhancing data security and code organization.

3.1 Example – Grouping Data and Behavior

Consider a class named Student, which encapsulates student-related data (String name, int marks, int ID_number, int age) and behavior (Read(), Write()). This grouping of data members into a single unit, the class Student, exemplifies encapsulation.

class Student {
  String name;
  int age;
  int ID_number;
  int marks;

  void Read() {}
  void Write() {}
}
Understanding Encapsulation in Java

Every Java class inherently embodies the concept of encapsulation. In essence, any component that adheres to data hiding and abstraction is termed an encapsulated component. Therefore, encapsulation can be viewed as a combination of data hiding and abstraction, denoted as:

Encapsulation = data hiding + abstraction

3.2 Tightly Encapsulated Class

A tightly encapsulated class is characterized by having every variable declared as private. In such classes, the focus is on the variable, and there is no need to delve into the method details. Here are a few scenarios to illustrate:

Case 1

In Case 1, the Account class is tightly encapsulated as the variable balance is declared as private.

public class Account{
	private double balance;
	public double getBalance(){
		// check for validation
		// if valid
		// return balance;
	}
}

Case 2

In Case 2, class A is a tightly encapsulated class, while class B is not, and class C is tightly encapsulated due to the private variable declarations.

//A tightly encapsulated class
class A{
	private int x = 10;
}
//Not a tightly encapsulated class
class B extends A{
	int y = 20;
}
// A tightly encapsulated class
class C extends A{
	private int z = 30;
}

Case 3

In Case 3, class A is not tightly encapsulated, and the same applies to class B due to the default variable declaration. However, class C is tightly encapsulated as it declares private variables, and its parent class class B does not expose default variables.

//Not a tightly encapsulated class
class A{
	int x = 10;
}
//Not a tightly encapsulated class
class B extends A{
	private int y = 20;
}
// A tightly encapsulated class
class C extends A{
	private int z = 30;
}

End Of Article

End Of Article