25 Oct 2023




Beginner

Abstraction is the process of hiding the internal details of an object and exposing only the essential functionalities and properties relevant to its use.

  • It focuses on what the object does (its interface) rather than how it does it (its implementation).
  • This allows users to interact with objects without needing to understand their intricate internal workings, simplifying code and improving maintainability.

💡Note: In the above defination

  • internal details of an object refer to the implementation details or the inner workings of the object that are not exposed to the users of the object
  • essential functionalities refer to the actions or behaviors that an object can perform.
  • properties refer to the attributes or characteristics of an object.

Abstraction in OOP is a concept where you focus on the essential qualities of an object while ignoring the non-essential details. It involves creating abstract classes or interfaces that define a clear, high-level blueprint for objects and their interactions.

Here are some key aspects of abstraction in OOP:

  • Data abstraction: Hiding the internal data of an object and only exposing methods for accessing and manipulating it.
  • Functional abstraction: Focusing on what an object can do (its functionalities) rather than how it does it (its implementation).
  • Interfaces: Defining a set of methods that an object must implement, without specifying how they are implemented.

The key components of abstraction in OOP include:

  1. Abstract Classes: These are classes that cannot be instantiated on their own and may contain abstract methods (methods without a body) that must be implemented by the concrete (non-abstract) subclasses.

  2. Interfaces: Interfaces define a contract that classes must adhere to by implementing the methods declared in the interface. They provide a way to achieve abstraction by specifying what an object must do without specifying how it should do it.

  3. Encapsulation: Abstraction often goes hand in hand with encapsulation, where the internal details of an object are hidden from the outside world. This helps in managing complexity and allows for changes to the internal implementation without affecting the external code.

Examples:

  1. Vehicle Abstraction:
    • Abstraction: The complex internal details of how a vehicle's engine, transmission, and electronics work are abstracted away.
    • Exposed: Only the essential features like accelerating, braking, and steering are exposed as abstract methods.
# Abstract Vehicle Class

- Attributes:
  - Color
  - Model

- Methods:
  - Accelerate()
  - Brake()
  - Steer()
  1. Bank Account Abstraction:
    • Abstraction: The intricate financial and database operations involved in managing a bank account are abstracted from the user.
    • Exposed: Users interact with simplified operations such as depositing, withdrawing, and checking the balance through abstract methods.
# Abstract Bank Account Class

- Attributes:
  - Account Number
  - Balance

- Methods:
  - Deposit(amount)
  - Withdraw(amount)
  - CheckBalance()
  1. Remote Control Abstraction:
    • Abstraction: The complex electronics and communication protocols involved in sending signals to a TV are hidden.
    • Exposed: Users press abstract buttons like the power, volume up, and channel down buttons to control the TV without knowing the internal mechanisms.
# Abstract Remote Control Class

- Attributes:
  - Power Button
  - Volume Up Button
  - Volume Down Button
  - Channel Up Button
  - Channel Down Button

- Methods:
  - PressPowerButton()
  - PressVolumeUpButton()
  - PressVolumeDownButton()
  - PressChannelUpButton()
  - PressChannelDownButton()
  1. Smartphone Interface Abstraction:
    • Abstraction: The intricate software and hardware interactions of a smartphone are abstracted.
    • Exposed: Users interact with abstract methods to open apps, make calls, send messages, and access the internet without knowledge of the smartphone's internal workings.
# Abstract Smartphone Class

- Attributes:
  - Screen
  - App Icons

- Methods:
  - OpenApp(appIcon)
  - MakeCall(contact)
  - SendTextMessage(contact, message)
  - AccessInternet()
  1. Library Catalog Abstraction:
    • Abstraction: The detailed library organization and book placement procedures are abstracted.
    • Exposed: Users search for books, check book availability, borrow books, and return books through abstract methods without needing to understand the library's internal catalog system.
# Abstract Library Catalog Class

- Attributes:
  - Catalog System

- Methods:
  - SearchForBook(title)
  - CheckAvailability(book)
  - BorrowBook(book)
  - ReturnBook(book)