What Do You Mean by Abstraction in Java?
- 4.0/5
- 686
- May 03, 2025
Abstraction in Java is an Object-Oriented Programming (OOP) concept that focuses on hiding implementation details and showing only the essential features of an object or system to the outside world.
Definition: Abstraction is the process of hiding the complex internal logic and exposing only the necessary parts through interfaces or abstract classes.
- Increase code reusability and scalability.
- Separate what an object does from how it does it.
- Can have both abstract (unimplemented) and concrete (implemented) methods.
- A class implements the interface and defines the behavior.
Real-World Analogy: Think of a TV remote - You know what buttons to press to change the channel (interface), but you don't need to know how the internal circuits work (implementation).
Definition: Abstraction is the process of hiding the complex internal logic and exposing only the necessary parts through interfaces or abstract classes.
Purpose of Abstraction:
- Reduce complexity.- Increase code reusability and scalability.
- Separate what an object does from how it does it.
How Abstraction is Achieved in Java
1) Using Abstract Classes:
- Cannot be instantiated.- Can have both abstract (unimplemented) and concrete (implemented) methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Animal { | |
abstract void makeSound(); // abstract method | |
void sleep() { | |
System.out.println("Sleeping..."); | |
} | |
} | |
class Dog extends Animal { | |
void makeSound() { | |
System.out.println("Bark!"); | |
} | |
} |
2) Using Interfaces:
- All methods are abstract by default (before Java 8).- A class implements the interface and defines the behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Vehicle { | |
void drive(); | |
} | |
class Car implements Vehicle { | |
public void drive() { | |
System.out.println("Driving a car."); | |
} | |
} |
Real-World Analogy: Think of a TV remote - You know what buttons to press to change the channel (interface), but you don't need to know how the internal circuits work (implementation).