← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 2 of 8

Java में Abstraction

Abstraction का मतलब है यह दिखाना कि कोई चीज़ क्या करती है और कैसे करती है छुपाना, electronics जाने बिना एक TV remote उपयोग करने जैसा। यह code को उपयोग करने में simpler रखता है।
Syntax
java
abstract class AbstractName {
    abstract returnType methodName();
}

interface InterfaceName {
    returnType methodName();
}

What is Abstraction?

Abstraction का मतलब है किसी चीज़ की complicated inner workings छुपाना और सिर्फ essential operations expose करना जो एक caller को असल में चाहिए — एक driver को सिर्फ accelerator press करना जानना है, यह नहीं कि engine की fuel injection असल में कैसे काम करती है।

उदाहरण: What is Abstraction?

java
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();
	}
}

Implementing Abstraction

Java आपको abstraction के लिए दो tools देता है: abstract classes, जो partial abstraction के लिए concrete और unimplemented methods mix कर सकती हैं, और interfaces, जो historically pure 100% abstraction enforce करते थे (हालांकि Java 8 के default methods ने वह line कुछ hद तक धुंधली कर दी है)।

उदाहरण: Implementing Abstraction

java
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();
	}
}

Abstracting Component Logic

Abstract blueprints define करना एक component के structural contract (कौन से methods मौजूद होने चाहिए) को इसके concrete operational details (वे methods असल में कैसे काम करते हैं) से अलग करता है, जो बड़े codebases को modular रखता है क्योंकि implementation details contract को छुए बिना बदल सकते हैं।

उदाहरण: Abstracting Component Logic

java
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);
	}
}

Abstracting Object Actions

अच्छी abstraction इस पर focus करती है कि एक object क्या करता है बजाय यह internally कैसे करता है, जो बिल्कुल वह है जो आपको बाद में उस abstract contract पर सिर्फ निर्भर किसी code को तोड़े बिना एक implementation swap करने या improve करने देता है।

उदाहरण: Abstracting Object Actions

java
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");
	}
}

Benefits of Abstraction

Abstraction का practical payoff है class उपयोग करने वाले किसी भी व्यक्ति के लिए reduced complexity, components के बीच loose coupling, और बाद में internal implementation details बदलने की freedom बिना हर caller के साथ बदलने की ज़रूरत के।

उदाहरण: Benefits of Abstraction

java
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");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Abstraction को encapsulation के साथ confuse करना, क्योंकि abstraction complexity छुपाता है और encapsulation data छुपाता है।
  2. Public API में implementation details expose करना।
  3. हर चीज़ के लिए abstract types बनाना, यहां तक कि जहां सिर्फ एक simple class चाहिए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.