Abstraction
Hiding implementation details
Overview
Abstraction is the concept of hiding complex implementation details and showing only the necessary features to the user. It focuses on "what" an object does rather than "how" it does it.
In OOP, abstraction is achieved through abstract classes and interfaces. It helps manage complexity by allowing you to work at a higher level without worrying about implementation details.
Key Concepts
Hide complexity, show essentials
Focus on what, not how
Achieved through abstract classes and interfaces
Reduces code complexity
Enables working at higher abstraction levels
Code Example
1// Abstract class providing abstraction
2public abstract class DatabaseConnection {
3 protected String connectionString;
4
5 // Template method - defines the workflow
6 public final void executeQuery(String query) {
7 connect();
8 validate(query);
9 runQuery(query);
10 disconnect();
11 }
12
13 // Abstract methods - implementation hidden
14 protected abstract void connect();
15 protected abstract void runQuery(String query);
16 protected abstract void disconnect();
17
18 // Concrete method with default implementation
19 protected void validate(String query) {
20 if (query == null || query.isEmpty()) {
21 throw new IllegalArgumentException("Query cannot be empty");
22 }
23 }
24}
25
26// Concrete implementation for MySQL
27public class MySQLConnection extends DatabaseConnection {
28 @Override
29 protected void connect() {
30 System.out.println("Connecting to MySQL database...");
31 // MySQL specific connection logic
32 }
33
34 @Override
35 protected void runQuery(String query) {
36 System.out.println("Executing MySQL query: " + query);
37 // MySQL specific query execution
38 }
39
40 @Override
41 protected void disconnect() {
42 System.out.println("Disconnecting from MySQL...");
43 }
44}
45
46// Concrete implementation for PostgreSQL
47public class PostgreSQLConnection extends DatabaseConnection {
48 @Override
49 protected void connect() {
50 System.out.println("Connecting to PostgreSQL database...");
51 }
52
53 @Override
54 protected void runQuery(String query) {
55 System.out.println("Executing PostgreSQL query: " + query);
56 }
57
58 @Override
59 protected void disconnect() {
60 System.out.println("Disconnecting from PostgreSQL...");
61 }
62}
63
64// Client code - works with abstraction
65public class Application {
66 public static void main(String[] args) {
67 // Client doesn't need to know connection details
68 DatabaseConnection db = new MySQLConnection();
69 db.executeQuery("SELECT * FROM users");
70
71 // Easy to switch databases
72 db = new PostgreSQLConnection();
73 db.executeQuery("SELECT * FROM users");
74 }
75}The client uses DatabaseConnection abstraction without knowing the specific database implementation details.
Advantages
- ✓
Reduces complexity for users
- ✓
Hides implementation details
- ✓
Enables code reuse
- ✓
Makes maintenance easier
- ✓
Allows changing implementation without affecting users
Real-World Example
Car Dashboard: Shows speed, fuel level, temperature - abstracts away engine mechanics, fuel injection, cooling system.
ATM Machine: Shows withdraw, deposit, balance options - hides banking protocols, network communication, security.
TV Remote: Power, volume, channel buttons - abstracts complex signal processing and electronics.
Best Practices
- 1.
Identify what users need vs. implementation details
- 2.
Use abstract classes for partial abstraction with shared code
- 3.
Use interfaces for complete abstraction
- 4.
Don't over-abstract - keep it practical
- 5.
Abstract at the right level for your use case
💡 Interview Tips
Explain difference between abstraction and encapsulation
Abstraction: what to show; Encapsulation: how to hide
Give real-world examples (ATM, car)
Show code example with abstract class or interface
Discuss when to use abstract class vs interface