← Back to Advanced Java Course | Chapter 1: Generics | Lesson 2 of 4

Java की Generic Classes

एक generic class एक blueprint है जिसमें वह type रखने के लिए खाली जगह है, जैसे एक lunchbox जिसमें sandwiches या fruit रखे जा सकते हैं। आप इसका उपयोग करते समय type भरते हैं।
Syntax
java
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

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

Multiple Type Parameters

Generic classes कई type parameters define कर सकती हैं जब एक placeholder पर्याप्त न हो, उन्हें angle brackets के अंदर commas से अलग करते हुए, उदाहरण के लिए <K, V> जैसा Map उपयोग करता है। जब class को वास्तव में instantiate किया जाता है तो हर parameter को अलग-अलग एक concrete type से replace किया जाता है।

उदाहरण: Multiple Type Parameters

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

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

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

Generic Interface Implementation

Classes generic interfaces को दो तरीकों में से एक से implement कर सकती हैं: आप या तो अपनी ही class definition में generic type parameter बनाए रख सकते हैं और खुद generic रह सकते हैं, या implementation के समय एक concrete data type specify कर सकते हैं और interface को सिर्फ उसी type पर lock कर सकते हैं।

उदाहरण: Generic Interface Implementation

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

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

java
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));
	}
}
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. किसी generic class के अंदर new T() या new T[10] लिखना, जो compile नहीं होता क्योंकि T runtime पर erase हो जाता है।
  2. type T का एक static field declare करना, जिसकी अनुमति नहीं है क्योंकि static members class के सभी instantiations द्वारा share किए जाते हैं।
  3. 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:

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.