Java Abstraction
In this page:
What is Abstraction?
Abstraction means hiding the complicated inner workings of something and exposing only the essential operations a caller actually needs — a driver only needs to know how to press the accelerator, not how the engine's fuel injection actually works.
Example: What is Abstraction?
abstract class Vehicle {
abstract void drive(); // hides how driving actually works
}
class Car extends Vehicle {
void drive() {
System.out.println("Pressing accelerator");
}
}
public class Main {
public static void main(String[] args) {
new Car().drive();
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Abstraction
Java gives you two tools for abstraction: abstract classes, which can mix concrete and unimplemented methods for partial abstraction, and interfaces, which historically enforced pure 100% abstraction (though Java 8's default methods have blurred that line somewhat).
Example: Implementing Abstraction
abstract class Shape {
abstract double area(); // abstract class: partial abstraction
}
interface Drawable {
void draw(); // interface: pure abstraction
}
class Circle extends Shape implements Drawable {
double radius = 2;
double area() { return Math.PI * radius * radius; }
public void draw() { System.out.println("Drawing circle"); }
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
System.out.println(c.area());
c.draw();
}
}
Login to try C/C++/Java/PHP code in the editor
Abstracting Component Logic
Defining abstract blueprints separates a component's structural contract (what methods must exist) from its concrete operational details (how those methods actually work), which keeps large codebases modular since implementation details can change without touching the contract.
Example: Abstracting Component Logic
interface PaymentProcessor {
void pay(double amount); // structural contract only
}
class CreditCardProcessor implements PaymentProcessor {
public void pay(double amount) {
System.out.println("Charging card: " + amount);
}
}
public class Main {
public static void main(String[] args) {
PaymentProcessor p = new CreditCardProcessor();
p.pay(50);
}
}
Login to try C/C++/Java/PHP code in the editor
Abstracting Object Actions
Good abstraction focuses on what an object does rather than how it does it internally, which is exactly what lets you swap out or improve an implementation later without breaking any code that depends only on the abstract contract.
Example: Abstracting Object Actions
interface Notifier {
void send(String message); // what it does, not how
}
class EmailNotifier implements Notifier {
public void send(String message) {
System.out.println("Emailing: " + message);
}
}
public class Main {
public static void main(String[] args) {
Notifier n = new EmailNotifier(); // swap implementation freely
n.send("Hello");
}
}
Login to try C/C++/Java/PHP code in the editor
Benefits of Abstraction
The practical payoff of abstraction is reduced complexity for anyone using the class, loose coupling between components, and the freedom to change internal implementation details later without every caller needing to change along with it.
Example: Benefits of Abstraction
interface Storage {
void save(String data);
}
class FileStorage implements Storage {
public void save(String data) {
System.out.println("Saved to file: " + data);
}
}
public class Main {
static void store(Storage storage, String data) {
storage.save(data); // caller doesn't care how storage works internally
}
public static void main(String[] args) {
store(new FileStorage(), "report.txt");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: