Β
π Abstraction in Java β Hiding Details, Showing Essentials
When you drive a car π, do you worry about how the engine works internally?
No! You only care about the steering wheel, brakes, accelerator, and gears.
π Thatβs exactly what Abstraction does in Java: it hides implementation details and shows only necessary functionality.
π What is Abstraction?
-
Definition: Abstraction is the process of hiding the internal details of an object and exposing only the essential features.
-
In Java, abstraction is achieved using:
β Abstract Classes (0β100% abstraction)
β Interfaces (100% abstraction, before Java 8; now supports default & static methods too)
Β
π What is an Abstract Class in Java?
An abstract class in Java is a special type of class that is incomplete and cannot be instantiated directly.
π Think of it as a blueprint for other classes.
Itβs used when you want to:
-
Define common properties and behaviors for all subclasses.
-
Leave some methods unimplemented (to be completed by subclasses).
β¨ Key Features of Abstract Class
-
Declared using the
abstractkeyword. -
Can have:
β Abstract methods β methods without a body (only declaration).
β Concrete methods β methods with a body (implementation). -
Cannot be used to create objects directly.
-
Must be extended by another class that provides implementation.
π οΈ Example: Abstract Class
// Abstract Class
abstract class Animal {
// Abstract method (no body)
abstract void sound();
// Concrete method
void sleep() {
System.out.println("Animal is sleeping");
}
}
// Subclass
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
// Animal a = new Animal(); β Not allowed (abstract class)
Animal a = new Dog(); // β
Polymorphism
a.sound(); // Dog barks
a.sleep(); // Animal is sleeping
}
}
Output:
Dog barks
Animal is sleeping
π― When to Use Abstract Class?
-
When you want to share common code among multiple subclasses.
-
When you want to force subclasses to implement certain methods.
-
When you need partial abstraction (some methods defined, some abstract).
π Real-World Example
-
Shape Class π¦:
-
Abstract Class:
Shape(common property: area, perimeter). -
Subclasses:
Circle,Rectangle,Triangleβ each implements its own way to calculate area.
-
π In short:
Abstract class = βTemplate + Common Codeβ
It allows flexibility while ensuring subclasses follow a certain structure.
Another example on abstract class:
abstract class Vehicle {
abstract void start(); // Abstract method (no body)
void fuel() { // Concrete method
System.out.println("Vehicle needs fuel");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with a key");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start(); // Calls Car's implementation
v.fuel();
}
}
Output:
Car starts with a key
Vehicle needs fuel
π What is an Interface in Java?
An interface in Java is like a contract or blueprint that defines a set of methods (without implementation) that a class must follow.
π Think of it as a 100% abstraction tool (before Java 8) β it tells what a class should do, not how it should do it.
β¨ Key Features of Interfaces
-
Declared using the
interfacekeyword. -
All methods are public and abstract by default (till Java 7).
-
From Java 8 onwards:
β Can have default methods (with body).
β Can have static methods. -
Supports multiple inheritance (a class can implement multiple interfaces).
-
Cannot have constructors (cannot be instantiated).
π οΈ Example: Interface in Action
// Interface
interface Animal {
void sound(); // abstract method
void sleep(); // abstract method
}
// Class implementing interface
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
public void sleep() {
System.out.println("Dog sleeps");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // β
Polymorphism
a.sound(); // Dog barks
a.sleep(); // Dog sleeps
}
}
Output:
Dog barks
Dog sleeps
π― When to Use Interface?
-
When you want to achieve abstraction but donβt need shared implementation.
-
When you want to implement multiple inheritance (since Java doesnβt support multiple class inheritance).
-
When different classes must follow the same contract, but may implement it differently.
π Real-World Example
-
Payment System π³:
-
Interface:
Payment(methods:pay(),refund()). -
Implementations:
CreditCard,PayPal,UPI. -
Each class defines how payment is processed, but all must follow the same contract.
-
π In short:
Interface = 100% abstraction + multiple inheritance support.
It defines a “what-to-do” list that every implementing class must complete.
Β
π― Key Points to Remember
-
Abstract class can have both abstract methods (without body) and concrete methods (with body).
-
Interface provides pure abstraction (all methods abstract by default, unless default/static).
-
Abstraction helps in security (hiding implementation), flexibility, and code reusability.
π Real-World Analogy
-
ATM Machine π³: You only see options like “Withdraw”, “Deposit”, “Check Balance” β You donβt see how it internally interacts with the bank server.
-
Mobile Phone π±: You just click a button to take a picture; the complex camera algorithms are abstracted away.
β¨ Bottom line: Abstraction = βDonβt tell me how, just tell me what.β
It makes code cleaner, modular, and user-friendly.
Β