Skip to content
C

Abstraction

Abstraction means showing only the essential features of something while hiding the internal, complicated details. In Java, this is achieved using abstract classes and interfaces.


1. What is Abstraction?

Abstraction means showing only the essential features of something while hiding the internal, complicated details. In Java, this is achieved using abstract classes and interfaces.

2. Why is it used?

It lets users of a class interact with it through a simple, clear set of actions, without needing to understand or worry about how those actions are actually implemented internally.

3. Real-Life Example

Think of driving a car using just the steering wheel, accelerator, and brake, without needing to understand the internal engine mechanics. Abstraction lets you use something effectively while hiding the complex internal working.

4. Syntax

java
abstract class ClassName { abstract void methodName(); // no body, just a declaration }

5. Example Program

java
abstract class Payment { abstract void pay(); } class CardPayment extends Payment { void pay() { System.out.println("Payment done using card"); } } public class AbstractionDemo { public static void main(String[] args) { Payment p = new CardPayment(); p.pay(); } }

Output:

Payment done using card

6. Key Points to Remember

  • Abstraction focuses on "what" an object does, hiding "how" it does it.
  • It's achieved in Java through abstract classes and interfaces.
  • Abstraction and Encapsulation are related but different: abstraction hides implementation complexity, encapsulation hides and protects data.