A generic class hierarchy is when a generic class is extended by another class (which can be generic or non-generic). Java allows:
- A generic class to extend another generic class.
- A generic class to extend a non-generic class.
- A non-generic class to extend a generic class.
This promotes code reuse, type safety, and flexibility in inheritance.
Syntax of a Generic Class Hierarchy
class Parent<T> {
// generic parent class
}
class Child<T> extends Parent<T> {
// inherits generic type
}
Code language: JavaScript (javascript)
You can also define a child with different or additional type parameters:
class Child<T, U> extends Parent<T> {
// T comes from Parent, U is new
}
Code language: JavaScript (javascript)
Program-1:Simple Generic Inheritance
class Box<T> { protected T item; public void setItem(T item) { this.item = item; } public T getItem() { return item; } } class ColorBox<T> extends Box<T> { private String color; public void setColor(String color) { this.color = color; } public String getColor() { return color; } } public class Test { public static void main(String[] args) { ColorBox<String> cb = new ColorBox<>(); cb.setItem("LotusJavaPrince"); cb.setColor("Blue"); System.out.println(cb.getItem() + " and " + cb.getColor() + " box."); } } /* LotusJavaPrince in and box. */
Program-2: Generic Subclass with Multiple Parameters
class Pair<K, V> { K key; V value; public void set(K key, V value) { this.key = key; this.value = value; } public String getDetails() { return "Key: " + key + ", Value: " + value; } } class ExtendedPair<K, V, T> extends Pair<K, V> { T extra; public void setExtra(T extra) { this.extra = extra; } public String getAllDetails() { return getDetails() + ", Extra: " + extra; } } public class TestHierarchy { public static void main(String[] args) { ExtendedPair<String, Integer, String> ep = new ExtendedPair<>(); ep.set("Mahesh", 101); ep.setExtra("Manager"); System.out.println(ep.getAllDetails()); } } /* Key: Mahesh, Value: 101, Extra: Manager */
Example 3: Non-Generic Subclass Extending a Generic Class
class GenericEmployee<T> { T id; public void setId(T id) { this.id = id; } public T getId() { return id; } } // Non-generic subclass fixes the type class StringEmployee extends GenericEmployee<String> { private String name; public void setName(String name) { this.name = name; } public void showInfo() { System.out.println("Name: " + name + ", ID: " + getId()); } } public class EmployeeTest { public static void main(String[] args) { StringEmployee emp = new StringEmployee(); emp.setName("LotusJavaPrince"); emp.setId("EMP123"); emp.showInfo(); } } /* Name: LotusJavaPrince, ID: EMP123 */
Program-4: Generic Class Extending a Non-Generic Class
class Department { String deptName; public Department(String name) { this.deptName = name; } public String getDeptName() { return deptName; } } class Employee<T> extends Department { private T empId; public Employee(String dept, T empId) { super(dept); this.empId = empId; } public void show() { System.out.println("Department: " + getDeptName() + ", ID: " + empId); } } public class Demo { public static void main(String[] args) { Employee<Integer> emp = new Employee<>("Finance", 2025); emp.show(); } } /* Department: Finance, ID: 2025 */
Generic class hierarchies:
- Support flexible inheritance
- Promote code reuse with type safety
- Can involve same or new type parameters in child classes
- Allow non-generic subclasses to fix generic types