Java की Anonymous Classes
In this page:
InterfaceOrClass variable = new InterfaceOrClass() {
@Override
public returnType methodName() {
// body
}
};
What is an Anonymous Class?
एक anonymous class आपको एक single expression में एक one-off subclass या interface implementation declare और instantiate करने देती है, इसे कोई नाम दिए बिना।
यह सबसे उपयोगी है जब आपको बिल्कुल एक बार उपयोग होने वाला एक छोटा, throwaway implementation चाहिए, जैसे एक quick event listener या comparator।
उदाहरण: What is an Anonymous Class?
interface Greeting {
void greet();
}
public class Main {
public static void main(String[] args) {
Greeting g = new Greeting() { // one-off implementation, no name
public void greet() {
System.out.println("Hi!");
}
};
g.greet();
}
}
Login to try C/C++/Java/PHP code in the editor
Anonymous Classes with Abstract Classes
आप instantiation के point पर इसके abstract methods की body inline supply करके एक abstract class को anonymously extend कर सकते हैं। यह उस logic के लिए एक अलग named subclass file लिखना skip करता है जिसे आप सिर्फ एक जगह उपयोग करेंगे।
उदाहरण: Anonymous Classes with Abstract Classes
abstract class Shape {
abstract void draw();
}
public class Main {
public static void main(String[] args) {
Shape s = new Shape() { // extends abstract class anonymously
void draw() {
System.out.println("Drawing shape");
}
};
s.draw();
}
}
Login to try C/C++/Java/PHP code in the editor
Anonymous Classes as Method Arguments
Anonymous classes आमतौर पर सीधे methods को arguments के रूप में pass की जाती हैं जो एक interface या abstract type expect करते हैं, जैसे Runnable या Comparator। यह implementation को call site के बिल्कुल बगल में रखता है बजाय codebase में बिखरे होने के।
उदाहरण: Anonymous Classes as Method Arguments
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Integer[] nums = {5, 2, 8};
Arrays.sort(nums, new Comparator<Integer>() { // passed directly as an argument
public int compare(Integer a, Integer b) {
return b - a;
}
});
System.out.println(Arrays.toString(nums));
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Local Variables
एक anonymous class अपने enclosing scope से local variables पढ़ सकती है, लेकिन सिर्फ अगर वे variables effectively final हों (initialization के बाद कभी reassigned न हों)।
Compiler creation time पर एक copy capture करता है, इसलिए class original variable में बाद के changes observe नहीं कर सकती।
उदाहरण: Accessing Local Variables
interface Printer {
void print();
}
public class Main {
public static void main(String[] args) {
final String message = "Hello"; // effectively final
Printer p = new Printer() {
public void print() {
System.out.println(message);
}
};
p.print();
}
}
Login to try C/C++/Java/PHP code in the editor
Limitations of Anonymous Classes
Anonymous classes का अपना named constructor नहीं हो सकता, एक साथ कई interfaces implement नहीं कर सकतीं, और body कुछ lines से बड़ी होने पर पढ़ना मुश्किल हो जाता है।
ज़्यादा complex किसी भी चीज़ के लिए, एक lambda (functional interfaces के लिए) या एक proper named class कहीं ज़्यादा clearly पढ़ती है।
उदाहरण: Limitations of Anonymous Classes
interface Task {
void run();
}
public class Main {
public static void main(String[] args) {
Task t = new Task() { // no named constructor, only one interface at a time
public void run() {
System.out.println("Running task");
}
};
t.run();
}
}
Login to try C/C++/Java/PHP code in the editor
- एक anonymous class में एक constructor जोड़ने की कोशिश करना, जो possible नहीं।
- एक anonymous class में उपयोग किए local variable बदलना, जिसे final या effectively final होना चाहिए।
- Closing brace के बाद semicolon भूल जाना।
- Polymorphism और abstraction एक interface को कई implementations के लिए stand करने देते हैं, abstract classes और interfaces उपयोग करते हुए।
- एक class कई interfaces implement कर सकती है, जबकि encapsulation internal data protect करता है।
- Inner और anonymous classes आपको दूसरे code के अंदर classes define करने देती हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: