Generic Class Hierarchies in Java
Last Updated :
17 May, 2021
Generic means parameterized types introduced in java5. These help in creating classes, interfaces, methods, etc. A class or method which works on parameterized type known as "generic class " or "generic method". Generics is a combination of language properties of the definition and use of Generic types and methods. Collections were used before Generics which holds any type of objects i.e. non-generic. Using Generics, it has become possible to create a single class, interface, or method that automatically works with all types of data(Integer, String, Float, etc). It has expanded the ability to reuse the code safely and easily. Generics also provide type safety (ensuring that an operation is being performed on the right type of data before executing that operation).
Hierarchical classifications are allowed by Inheritance. Superclass is a class that is inherited. The subclass is a class that does inherit. It inherits all members defined by super-class and adds its own, unique elements. These uses extends as a keyword to do so.
Sometimes generic class acts like super-class or subclass. In Generic Hierarchy, All sub-classes move up any of the parameter types that are essential by super-class of generic in the hierarchy. This is the same as constructor parameters being moved up in a hierarchy.
Example 1: Generic super-class
Java
// Java Program to illustrate generic class hierarchies
// Importing all input output classes
import java.io.*;
// Helper class 1
// Class 1 - Parent class
class Generic1<T> {
// Member variable of parent class
T obj;
// Constructor of parent class
Generic1(T o1) { obj = o1; }
// Member function of parent class
// that returns an object
T getobj1() { return obj; }
}
// Helper class 2
// Class 2 - Child class
class Generic2<T, V> extends Generic1<T> {
// Member variable of child class
V obj2;
Generic2(T o1, V o2)
{
// Calling super class using super keyword
super(o1);
obj2 = o2;
}
// Member function of child class
// that returns an object
V getobj2() { return obj2; }
}
// Class 3 - Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating Generic2 (sub class) object
// Custom inputs as parameters
Generic2<String, Integer> x
= new Generic2<String, Integer>("value : ",
100);
// Calling method and printing
System.out.println(x.getobj1());
System.out.println(x.getobj2());
}
}
Note: A subclass can freely add its own type parameters, if necessary.
Example 2: Non-generic sub-class of generic sub-class
Java
import java.io.*;
// non-generic super-class
class NonGen {
int n;
NonGen(int i) { n = i; }
int getval() { return n; }
}
// generic class sub-class
class Gen<T> extends NonGen {
T obj;
Gen(T o1, int i)
{
super(i);
obj = o1;
}
T getobj() { return obj; }
}
class GFG {
public static void main(String[] args)
{
Gen<String> w = new Gen<String>("Hello", 2021);
System.out.println(w.getobj() + " " + w.getval());
}
}
Similar Reads
Generic Class in Java Java Generics was introduced to deal with type-safe objects. It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types. Generics also provide compile-time type safety which allows programmers to c
4 min read
Generics in Java Generics means parameterized types. The idea is to allow a type (like Integer, String, etc., or user-defined types) to be a parameter to methods, classes, and interfaces. Generics in Java allow us to create classes, interfaces, and methods where the type of the data is specified as a parameter. If w
10 min read
Generic Constructors and Interfaces in Java Generics make a class, interface and, method, consider all (reference) types that are given dynamically as parameters. This ensures type safety. Generic class parameters are specified in angle brackets â<>â after the class name as of the instance variable. Generic constructors are the same as
5 min read
Ennead Class in JavaTuples A Ennead is a Tuple from JavaTuples library that deals with 9 elements. Since this Ennead is a generic class, it can hold any type of value in it. Since Ennead is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read
Decade Class in JavaTuples A Decade is a Tuple from JavaTuples library that deals with 3 elements. Since this Decade is a generic class, it can hold any type of value in it.Since Decade is a Tuple, hence it also has all the characteristics of JavaTuples:Â Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read