Java की Generic Classes
In this page:
class ClassName<T> {
private T field;
public void setField(T value) { this.field = value; }
public T getField() { return field; }
}
Defining a Generic Class
एक generic class define करने के लिए, अपने class name के ठीक बाद angle brackets में एक type parameter जोड़ें, जैसे <T>। फिर आप class body में कहीं भी fields, constructors, और method signatures define करने के लिए उस parameter का उपयोग कर सकते हैं।
उदाहरण: Defining a Generic Class
// Define the class Main
public class Main {
// Define the class Box
static class Box<T> {
private T content;
Box(T content) { this.content = content; }
T getContent() { return content; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Box object and store it in intBox
Box<Integer> intBox = new Box<>(99);
// Print a line to the console
System.out.println(intBox.getContent());
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Type Parameters
Generic classes कई type parameters define कर सकती हैं जब एक placeholder पर्याप्त न हो, उन्हें angle brackets के अंदर commas से अलग करते हुए, उदाहरण के लिए <K, V> जैसा Map उपयोग करता है। जब class को वास्तव में instantiate किया जाता है तो हर parameter को अलग-अलग एक concrete type से replace किया जाता है।
उदाहरण: Multiple Type Parameters
// Define the class Main
public class Main {
// Define the class Pair
static class Pair<K, V> {
K key; V value;
Pair(K key, V value) { this.key = key; this.value = value; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Pair object and store it in pair
Pair<String, Integer> pair = new Pair<>("Age", 30);
// Print a line to the console
System.out.println(pair.key + " -> " + pair.value);
}
}
Login to try C/C++/Java/PHP code in the editor
Bounded Type Parameters
आप bounded type parameters का उपयोग करके अपनी generic class में अनुमत types को सीमित कर सकते हैं, जो तब उपयोगी है जब आपकी class को सिर्फ कुछ types पर मौजूद methods call करने हों। extends keyword parameter को एक class या interface और उसके subtypes तक सीमित करता है, compiler को उन method calls की अनुमति देने के लिए पर्याप्त जानकारी देता है।
उदाहरण: Bounded Type Parameters
// Define the class Main
public class Main {
// Define the class NumberBox
static class NumberBox<T extends Number> {
private T num;
NumberBox(T num) { this.num = num; }
double doubled() { return num.doubleValue() * 2; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new NumberBox object and store it in box
NumberBox<Integer> box = new NumberBox<>(10);
// Print a line to the console
System.out.println(box.doubled());
}
}
Login to try C/C++/Java/PHP code in the editor
Generic Interface Implementation
Classes generic interfaces को दो तरीकों में से एक से implement कर सकती हैं: आप या तो अपनी ही class definition में generic type parameter बनाए रख सकते हैं और खुद generic रह सकते हैं, या implementation के समय एक concrete data type specify कर सकते हैं और interface को सिर्फ उसी type पर lock कर सकते हैं।
उदाहरण: Generic Interface Implementation
// Define the class Main
public class Main {
// Define the interface Container
interface Container<T> {
T get();
}
// Define the class StringContainer
static class StringContainer implements Container<String> {
public String get() { return "Fixed to String"; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new StringContainer object and store it in c
Container<String> c = new StringContainer();
// Print a line to the console
System.out.println(c.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Raw Types and Warnings
किसी generic class को बिना type parameter specify किए उपयोग करना वह बनाता है जिसे 'raw type' कहा जाता है, एक legacy behavior जो pre-Generics code के साथ backward compatibility के लिए रखा गया है। जबकि एक raw type अब भी compile होती है, यह compiler warnings को trigger करती है और Generics के सभी type-safety फायदे खो देती है, इसलिए नए code में इससे बचना चाहिए।
उदाहरण: Raw Types and Warnings
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List rawList = new ArrayList(); // raw type -- compiles but triggers a warning
rawList.add("no type safety");
System.out.println(rawList.get(0));
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी generic class के अंदर
new T()याnew T[10]लिखना, जो compile नहीं होता क्योंकिTruntime पर erase हो जाता है। - type
Tका एकstaticfield declare करना, जिसकी अनुमति नहीं है क्योंकि static members class के सभी instantiations द्वारा share किए जाते हैं। - type argument के बिना raw
Boxका उपयोग करना, जो एक unchecked warning के साथ compile होता है और बाद मेंClassCastExceptionके साथ fail हो सकता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: