Java Intermediate

0% completed

Previous
Next
Generic Classes

Generic classes allow you to write classes that can operate on objects of various types while providing compile-time type safety. Instead of specifying a concrete type, a generic class uses type parameters as placeholders. This makes your code more flexible and reusable. Generic classes are widely used in the Java Collections Framework and other parts of the language.

Syntax for Defining a Generic Class

public class ClassName<T> { // Field of generic type private T value; // Constructor to initialize the generic field public ClassName(T value) { this.value = value; } // Getter method for the generic field public T getValue() { return value; } // Setter method for the generic field public void setValue(T value) { this.value = value; } }
  • Explanation:
    • ClassName<T> declares a generic class with a type parameter T.
    • The field value is of type T and can hold any object of the type specified when an instance is created.
    • The constructor initializes the generic field, while getter and setter methods allow you to retrieve and modify its value.

Syntax for Creating an Instance of Generic Class

Box<Integer> intBox = new Box<>(123);
  • Explanation: Here, Integer is referred as a generic type and "123" is a value passed as an argument.

Examples

Example 1: Simple Generic Class (Box)

This example demonstrates a generic class called Box that can store an object of any type. We then create instances of Box for different types (e.g., Integer and String) and use the getter method to display the stored values.

Java
Java

. . . .

Example Explanation

  • Generic Class Definition:

    • The Box<T> class is defined with a type parameter T.
    • It has a private field content of type T and provides getter and setter methods.
  • Creating Instances:

    • An instance of Box<Integer> is created to store an integer value (123).
    • An instance of Box<String> is created to store a string ("Hello Generics!").
  • Output:

    • The getContent() method retrieves and prints the stored values, demonstrating type safety and reusability.

Example 2: Generic Class with Multiple Type Parameters

This example illustrates a generic class named Pair that accepts two type parameters, K and V, to store a key-value pair. We create an instance of Pair and print both the key and the value.

Java
Java

. . . .

Example Explanation:

  • Generic Class with Multiple Parameters:
    • The Pair<K, V> class uses two type parameters to store a key and a value.
  • Methods Provided:
    • Getter and setter methods for both key and value allow access and modification.
  • Instance Creation:
    • A Pair<String, Integer> instance is created with key "Age" and value 30.
  • Output:
    • The key and value are retrieved using getter methods and printed, illustrating the class's flexibility.

Generic classes in Java allow you to create flexible, reusable, and type-safe classes that work with various data types. By parameterizing types with placeholders (like T, K, V), you can write code that is independent of any specific type and catch type errors at compile time. These techniques are foundational for building robust and reusable components in Java applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next