๐๐ฏ๐๐๐ฟ๐ฎ๐ฐ๐๐ถ๐ผ๐ป ๐ถ๐ป ๐๐ฎ๐๐ฎ
Abstraction hides internal details. It shows only essential features to the user. It focuses on what an object does instead of how it does it.
Think about an ATM. You insert your card. You enter your PIN. You press withdraw. You get your cash. You do not see the internal code or the mechanical process inside the machine. That is abstraction.
Think about a car. You use the steering wheel, the accelerator, and the brakes. You do not need to understand how the engine or fuel injection works to drive. The interface is simple. The complexity is hidden.
In Java, you implement abstraction in two ways:
- Abstract Classes: These provide partial abstraction.
- Interfaces: These provide full abstraction for behavior.
Rules for Abstract Classes:
- An abstract class cannot be instantiated. You cannot create an object of it directly.
- If a class has one abstract method, the entire class must be declared abstract.
- Subclasses must implement the abstract methods to use them.
Example Scenario:
Imagine a class called Indians. It has an abstract method called haveBreakfast. You cannot define breakfast in the parent class because different regions eat different foods.
A subclass like Tamils will implement this method by printing Idly or Dosa. This allows the parent class to set a rule without defining the specific details.
Before you study abstraction, make sure you understand these OOP concepts:
- Class
- Object
- Inheritance
- Polymorphism
- Encapsulation
These topics work together. You need them to understand how abstraction functions in real code.
Source: https://www.geeksforgeeks.org/java/abstraction-in-java-2/
Optional learning community: https://dev.to/hariharan_sj_2003/abstraction-in-java-3n9i