CAB201

icon picker
Abstraction and Encapsulation

Abstraction

Abstraction is the process of hiding complex implementation details and showing only the essential features of an object or system. It allows you to focus on what an object does rather than how it does it.
Key aspects of abstraction:
Simplifies complex systems by breaking them down into more manageable parts.
Hides unnecessary details from the user.
Allows for the creation of generalized interfaces.
For example, when driving a car, you interact with the steering wheel, pedals, and gear shift without needing to understand the complex mechanics underneath.

Encapsulation

Encapsulation is the bundling of data and the methods that operate on that data within a single unit or object. It restricts direct access to some of an object's components, which is a means of preventing accidental interference and misuse of the methods and data.
Key aspects of encapsulation:
Combines data and behavior into a single unit.
Protects the internal state of an object.
Implements information hiding through access modifiers.

Relationship between Abstraction and Encapsulation

While abstraction and encapsulation are distinct concepts, they are closely related and often work together in object-oriented programming:
Complementary concepts: Abstraction focuses on the outside view of an object (what it does), while encapsulation focuses on the internal implementation (how it does it).
Implementation of abstraction: Encapsulation is often used as a mechanism to implement abstraction. By hiding internal details and providing a public interface, encapsulation supports the creation of abstractions.
Information hiding: Both concepts contribute to information hiding, which is crucial for managing complexity in software systems.
Design level vs. implementation: Abstraction is primarily a design-level process, while encapsulation is an implementation process.
Modularity and maintenance: Together, they promote modularity and ease of maintenance by allowing changes to the internal implementation without affecting the external interface.
Example in C#:
// Abstraction through an interface
public interface IVehicle
{
void Start();
void Stop();
}

// Encapsulation and implementation of the abstraction
public class Car : IVehicle
{
private bool isRunning; // Encapsulated state

public void Start()
{
if (!isRunning)
{
isRunning = true;
Console.WriteLine("Car started");
}
}

public void Stop()
{
if (isRunning)
{
isRunning = false;
Console.WriteLine("Car stopped");
}
}
}
In this example, IVehicle provides an abstraction of a vehicle's basic operations. The Car class encapsulates the implementation details and internal state (isRunning) while adhering to the abstraction defined by the interface.
By using abstraction and encapsulation together, you can create more modular, maintainable, and flexible code. Abstraction allows you to define clear interfaces and hide complexity, while encapsulation ensures that the internal workings of objects are protected and can be changed without affecting the rest of the system.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.