Java में Abstraction
In this page:
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?
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 आपको abstraction के लिए दो tools देता है: abstract classes, जो partial abstraction के लिए concrete और unimplemented methods mix कर सकती हैं, और interfaces, जो historically pure 100% abstraction enforce करते थे (हालांकि Java 8 के default methods ने वह line कुछ hद तक धुंधली कर दी है)।
उदाहरण: 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
Abstract blueprints define करना एक component के structural contract (कौन से methods मौजूद होने चाहिए) को इसके concrete operational details (वे methods असल में कैसे काम करते हैं) से अलग करता है, जो बड़े codebases को modular रखता है क्योंकि implementation details contract को छुए बिना बदल सकते हैं।
उदाहरण: 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
अच्छी abstraction इस पर focus करती है कि एक object क्या करता है बजाय यह internally कैसे करता है, जो बिल्कुल वह है जो आपको बाद में उस abstract contract पर सिर्फ निर्भर किसी code को तोड़े बिना एक implementation swap करने या improve करने देता है।
उदाहरण: 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
Abstraction का practical payoff है class उपयोग करने वाले किसी भी व्यक्ति के लिए reduced complexity, components के बीच loose coupling, और बाद में internal implementation details बदलने की freedom बिना हर caller के साथ बदलने की ज़रूरत के।
उदाहरण: 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
- Abstraction को encapsulation के साथ confuse करना, क्योंकि abstraction complexity छुपाता है और encapsulation data छुपाता है।
- Public API में implementation details expose करना।
- हर चीज़ के लिए abstract types बनाना, यहां तक कि जहां सिर्फ एक simple class चाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: