Contents
Encapsulation
Encapsulation in C++
Abstraction and Encapsulation in Object-Oriented Programming (OOP)
Abstraction and Encapsulation are key principles in Object-Oriented Programming (OOP). They are fundamental to building maintainable, reusable, and secure applications. Both concepts contribute to features like reusability, security, data hiding, and implementation concealment. Despite their relation, they serve different purposes and are implemented in unique ways. Let’s explore these differences with code examples.
Encapsulation in Java
Encapsulation is the process of bundling data (variables) and methods that operate on the data within a single unit or class. It ensures that the data is not accessible directly outside the class, thereby providing security and control over its modification. By restricting access to data through private variables and allowing controlled access via public methods, encapsulation prevents unauthorized access.
In simple terms, encapsulation can be thought of as a protective shield that keeps data safe from unauthorized access. This is why it is also referred to as data hiding.
Java Example of Encapsulation:
// Java program demonstrating encapsulation
class Student {
// Private variables, accessible only through public methods
private String studentName;
private int studentID;
private int studentAge;
// Getter method for age
public int getAge() {
return studentAge;
}
// Getter method for name
public String getName() {
return studentName;
}
// Getter method for ID
public int getID() {
return studentID;
}
// Setter method for age
public void setAge(int newAge) {
studentAge = newAge;
}
// Setter method for name
public void setName(String newName) {
studentName = newName;
}
// Setter method for ID
public void setID(int newID) {
studentID = newID;
}
}
public class TestEncapsulation {
public static void main(String[] args) {
// Creating an object of Student class
Student obj = new Student();
// Setting values of the variables
obj.setName("John");
obj.setAge(22);
obj.setID(1001);
// Displaying values of the variables
System.out.println("Student's name: " + obj.getName());
System.out.println("Student's age: " + obj.getAge());
System.out.println("Student's ID: " + obj.getID());
}
}
Output:
Student's name: John
Student's age: 22
Student's ID: 1001
In this example, encapsulation ensures that studentID
, studentName
, and studentAge
can only be accessed or modified through the setter and getter methods, not directly.
Abstraction in Java
Abstraction is the concept of showing only the relevant details to the user and hiding unnecessary implementation details. It allows you to focus on what an object does rather than how it does it. Abstraction simplifies complex systems by breaking them into smaller, more understandable parts.
For example, you interact with a vehicle through its interface (like driving a car), but you do not need to know the internal mechanics of how the engine operates. Abstraction is often achieved in Java through abstract classes and interfaces.
Java Example of Abstraction:
// Java program demonstrating abstraction
abstract class Appliance {
String brand;
// Abstract methods that subclasses need to implement
abstract void turnOn();
abstract void turnOff();
// Constructor
public Appliance(String brand) {
this.brand = brand;
}
// Concrete method
public String getBrand() {
return brand;
}
}
class WashingMachine extends Appliance {
public WashingMachine(String brand) {
super(brand);
}
@Override
void turnOn() {
System.out.println(brand + " Washing Machine is now ON");
}
@Override
void turnOff() {
System.out.println(brand + " Washing Machine is now OFF");
}
}
class Refrigerator extends Appliance {
public Refrigerator(String brand) {
super(brand);
}
@Override
void turnOn() {
System.out.println(brand + " Refrigerator is now ON");
}
@Override
void turnOff() {
System.out.println(brand + " Refrigerator is now OFF");
}
}
public class TestAbstraction {
public static void main(String[] args) {
Appliance washingMachine = new WashingMachine("LG");
Appliance refrigerator = new Refrigerator("Samsung");
washingMachine.turnOn();
refrigerator.turnOn();
washingMachine.turnOff();
refrigerator.turnOff();
}
}
Output:
LG Washing Machine is now ON
Samsung Refrigerator is now ON
LG Washing Machine is now OFF
Samsung Refrigerator is now OFF
In this example, abstraction allows us to define the basic behavior of an appliance without showing the internal workings. The specific implementations for turnOn
and turnOff
are provided by the subclasses WashingMachine
and Refrigerator
.
Differences Between Abstraction and Encapsulation
Abstraction | Encapsulation |
---|---|
Abstraction focuses on hiding implementation details while showing only essential features to the user. | Encapsulation bundles the data and methods that operate on the data within one unit and restricts direct access to some of the object’s components. |
It is used to reduce complexity and increase maintainability by defining clear interfaces. | It is used to hide data and provide controlled access, enhancing security and data integrity. |
Abstraction is more about the design and involves creating abstract classes and interfaces. | Encapsulation is more about the implementation, using access modifiers like private , protected , and public . |
Problems are solved at the interface level. | Problems are solved at the class level. |
Abstraction can be achieved using abstract classes and interfaces. | Encapsulation is achieved using access modifiers and getter/setter methods. |
Example: A TV remote control lets you operate the TV without showing how the circuits inside work. | Example: A person’s bank account details are hidden from unauthorized users, and access is granted only via public methods. |