Generic Methods and Classes Java In Hindi
जावा प्रोग्रामिंग लैंग्वेज में जेनेरिक्स जावा यूजर डेवलपर को एक ऐसा कस्टम प्रोग्राम सोर्स कोड क्रिएट करने में हेल्प करते हैं, जो अलग-अलग यूजर डिफाइन कस्टम डेटा टाइप एलिमेंट के साथ काम करता है, और जावा यूजर को डाटा टाइप सेफ्टी फीचर भी प्रोवाइड करता है।

जावा 5 वर्जन में जेनेरिक्स कांसेप्ट को स्टार्ट किया गया था।
जैसे, Integer, String, Double, आदि डाटा टाइप के लिए अलग-अलग यूजर डिफाइन क्लास क्रिएट करने के बदले, जावा यूजर एक जेनेरिक क्लास डेवलप कर सकते हैं।
Generic Class Concept in Java.
जावा प्रोग्राम में एक जेनेरिक क्लास वह यूजर डिफाइन कस्टम क्लास होती है, जो अलग-अलग डेटा टाइप मेथड एलिमेंट वेरिएबल के साथ काम कर सकती है।
Syntax of a Generic Class.
class ClassName<T> {
// here we can define a custom code
}
यहाँ, यूजर डिफाइन कॉल्स में T एक टाइप पैरामीटर मेथड है।
Example of a Generic Class.
class Shape<T> {
private T data;
public void setdata(T data) {
this.data = data;
}
public T getdata() {
return data;
}
}
//Java users can use the same class with different data types.
public class Main {
public static void main(String[] args) {
Shape<Integer> intShape = new Shape<>();
intShape.setdata(78910);
System.out.println(intShape.getdata());
Shape<String> stringShape = new Shape<>();
stringShape.setdata(“Vcanhelpsu”);
System.out.println(stringShape.getdata());
}
}
Its output.
78910
Vcanhelpsu
Here in this example.
- Shape<Integer> → इसमें T क्लास, Integer डाटा टाइप के रूप में कन्वर्ट होता है.
- Shape<String> → इसमें T क्लास, एक String डाटा टाइप के रूप में कन्वर्ट होता है.
Java’s generic class method with multiple type parameters.
जावा प्रोग्राम में एक क्लास में एक से ज़्यादा यूजर डिफाइन कस्टम डाटा टाइप पैरामीटर एलिमेंट डिक्लेअर या यूज़ हो सकते हैं।
Example of generic class method with multiple type parameters.
class Group<P, Q> {
private P key;
private Q data;
Group(P key, Q data) {
this.key = key;
this.data = data;
}
public P getKey() {
return key;
}
public Q getdata() {
return data;
}
}
// here we use multiple data type parameter method
public class Main {
public static void main(String[] args) {
Group<Integer, String> company =
new Group<>(78113442, “Vcanhelpsu Edtech”);
System.out.println(company.getKey());
System.out.println(company.getdata());
}
}
Its output.
78113442
Vcanhelpsu Edtech
Here in this example.
- P → यह एक इंटीजर डाटा टाइप मेथड को रिप्रेजेंट करता है.
- Q → यह एक स्ट्रिंग डाटा टाइप मेथड को रिप्रेजेंट करता है.
Common java generic class data type naming conventions method.
| # | Data Type | Data type Meaning |
| 1. | T | Use for data Type |
| 2. | E | Use for Element |
| 3. | K | Use for data Key |
| 4. | V | Use for data Value |
| 5. | N | Use for data Number |
Generic method concept in Java.
जावा प्रोग्राम में एक जेनेरिक क्लास मेथड एक ऐसा कांसेप्ट मेथड है, जो यूजर डिफाइन कस्टम अलग-अलग डेटा टाइप क्लास एलिमेंट वेरिएबल के साथ काम कर सकता है।
Syntax of a generic method.
<T> returnType methodName(T value) {
// here we can define a custom code
}
यहाँ इस एक्साम्प्ल में क्लास रिटर्न डाटा टाइप से पहले <T> टाइप पैरामीटर को डिक्लेयर करता है।
Example of a generic method.
class Sample {
public static <T> void preview(T data) {
System.out.println(data);
}
}
//Here we can call it from different class data type methods.
public class Main {
public static void main(String[] args) {
Sample.preview(10912);
Sample.preview(“Vcanhelpsu”);
Sample.preview(123.34);
}
}
Its output.
10912
Vcanhelpsu
123.34
यहाँ इस एक्साम्प्ल में कंपाइलर T के लिए करेक्ट डाटा टाइप के रूप में फिक्स करता है।
Concept of a generic method with a return data type value in Java.
public static <T> T getValue(T value) {
return value;
}
Usage of a generic method with a return data type.
String comp_name = getData(“Vcanhelpsu”);
Integer integers = getData(180);
System.out.println(comp_name);
System.out.println(integers);
यहाँ इस एक्साम्प्ल में, यह एक ही क्लास मेथड में String और Integer दोनों प्रकार के डाटा टाइप के साथ काम करता है।
Multiple parameter generic method concept in Java.
जावा प्रोग्राम में एक यूजर डिफाइन कस्टम क्लास डाटा टाइप मेथड में एक से अधिक कई डाटा टाइप पैरामीटर मैन्युअली डिफाइन हो सकते हैं।
public static <K, V> void preview(K key, V value) {
System.out.println(key + ” = ” + value);
}
जावा में इसका यूज़।
preview(109, “Vcanhelpsu”);
preview(“Cont”, 9413#######);
Its output.
109 = Vcanhelpsu
Cont = 9413#######
The bounded generic class data type concept.
जावा प्रोग्राम में कभी-कभी जावा यूजर यूज़ किए जा सकने वाले डाटा टाइप को लिमिटेड यूज़ करना चाहते हैं।
जैसे, हम यह बता सकते हैं कि T, integers डाटा का सबक्लास होना चाहिए।
public static <T extends integers> void preview(T data) {
System.out.println(data);
}
अब इस एक्साम्प्ल में ये मान्य हैं.
preview(112);
preview(89.19);
लेकिन इसमें।
preview(“Vcanhelpsu”); //
क्योंकि यहाँ String, डाटा टाइप integers डाटा टाइप को एक्सटेंड नहीं करता है।
Generic class methods with bounded data types in Java.
class IntegerShape<T extends Number> {
private T data;
IntegerShape(T data) {
this.data = data;
}
public T getdata() {
return data;
}
}
public class Main {
public static void main(String[] args) {
// here we set some Valid examples
IntegerShape<Integer> Shape1 = new IntegerShape<>(78);
IntegerShape<Double> Shape2 = new IntegerShape<>(34.87);
System.out.println(Shape 1.getdata());
System.out.println(Shape.getdata());
// here is a Invalid example (display compilation error)
// IntegerShape<String> box = new IntegerShape<>(“Vcanhelpsu”);
}
}
Advantages of generic data type const in Java.
Data type safety in Java.
जावा में जेनेरिक डाटा टाइप क्लास मेथड कंपाइल टाइम पर फाल्स डाटा टाइप को फाइंड कर सकते हैं।
List<String> comp_names = new ArrayList<>();
comp_names.add(“Vcanhelpsu”);
comp_names.add(199089); // यह जावा में एक कंपाइल-टाइम एरर को डिस्प्ले करता है
Code reusability in Java.
एक ही जेनेरिक क्लास या मेथड अलग-अलग डाटा टाइप के साथ काम कर सकता है। इसमें आपको क्लियर डाटा टाइप कास्टिंग की ज़रूरत नहीं
Without a generic data type.
Object value = “Vcanhelpsu”;
String comp_name = (String) value;
With generics.
Shape<String> shape = new Shape <>();
String comp_name = Shape.getValue();
Detail Difference Among Generic Class Vs Generic Method
| # | Generic java class method | Generic java method |
| 1. | In this data type parameter belongs to the class structure | In this data type parameter belongs to the user define method |
| 2. | Here it declared after a class name | Here it declared before return type method |
| 3. | Declare with class box<t> | It declare with <t> t method(t value) |
| 4. | In this data type can be used throughout the class | In this data type is generally limited to the method |
Generic Class in Java.
class Shape <T> {
T class value;
}
Generic Class Method.
<T> void preview(T value) {
System.out.println(value);
}
An easy way to remember generic methods and classes in Java.
जेनेरिक क्लास विथ जावा
↓
क्लास बॉक्स<T>
↓
यहाँ T का यूज़ कम्पलीट क्लास में किया जा सकता है
Generic Class Method
↓
<T> void display(T value)
↓
T is associated with that method.
Conclusion of Generic Methods and Classes.
जावा प्रोग्राम में जेनेरिक्स क्लास और मेथड को बिना किसी गैर-ज़रूरी डाटा टाइप कास्टिंग के अलग-अलग क्लास डेटा टाइप के साथ काम करने की परमिशन प्रोवाइड कर डाटा टाइप सेफ्टी, सोर्स कोड रीयूज़ेबिलिटी और फ्लेक्सिबिलिटी फीचर्स को प्रोवाइड करते हैं।

