Table of Contents
Introduction
JAVA Generics was introduced into the language with the advent of JDK 5. The main idea behind generalizing argument lists and datatypes of classes, interfaces, and objectives came from the fact that it engaged in greater inclusivity. Developers would no longer have to abide by non-flexible datatypes. Generic classes gave way to data structures like HashSet, ArrayList, HashMap, List, and so on.
What is a JAVA Generics Class?
A generic class in JAVA (or generic interface) can be thought of as a class that does not require the developer to specify the datatype that it will be handling. These classes will allow data manipulation of all primitive types without giving an error of type mismatch.
The Syntax to declare a class generic is: class XYZ <T> where T stands for the type, which is generic in this case.
A simple JAVA generic class construct would be as follows:
class Sample <T> { private T var; public void set (T var) { this.var = var; } public T get() { return var; }
If you want to create an instance of a generic class, you can use the following syntax:
BaseType <Type> obj = new BaseType <Type> ();
Note that you cannot specify a primate datatype in the above line while instantiating a Generic Class.
Simple Code involving a JAVA Generics Class
//In this following code, we will just be printing out two lines using two different data types. class Sample <T> //< > is used to specify the parameter type. T means a generic class. { T obj; //Constructor Sample (T obj) { this.obj = obj; } public T get() { //method call to get the value of the object return this.obj; } class Body { public static void main (String [] args ) { //We will now create two objects of the generic class but with different datatypes. Sample <Integer> obj1 = new Sample <Integer> (20); Sample <String> obj2 = new Sample <String> (“Test Successful”); System.out.println(obj1.get()); System.out.println(obj2.get()); } }
OUTPUT
20
Test Successful
Generic Functions
Generic Functions, like generic classes, will not have a fixed datatype. These methods can be called using different argument lists. The list adapts to the argument type that is passed on to it. With the help of the following example, the theory will be even clearer.
class Sample { // We will be creating a static generic method here. ‘<T>’ symbolizes that the method does not have //a specified return type. static <T> void genMethod (T variable) { //for users to better understand what datatype is being printed out, this print statement is //imperative. System.out.println (variable.getClass().getName() + “=” + variable); public static void main (String [] args) { //We will now call the generic method by passing an Integer value genMethod (5); //we will now call the generic method by passing a String genMethod (“Test Successful”); //finally we will call the general method by passing a double value genMethod (9.0); } }
OUTPUT
java.lang.Integer = 5
java.lang.String = Test Successful
java.lang.Double = 1.0
Generic Constructor
A generic constructor initializes the variables in a generic class. It goes without saying that generic constructors can only be used in generic classes.
Following is n example of a generic constructor in use inside a generic class.
class StudentRegistry <T> { private T Student_ID ; private T Student_Roll ; private T Grade ; //Below is the generic constructor public StudentRegistry (T Student_ID, T Student_Roll, T Grade) { super (); this.Student_ID = Student_ID; this.Student_Roll = Student_Roll; this.Grade = Grade; } }
Advantage of using Generics in JAVA
One of the several advantages of using generics is that it gives you the freedom to reuse codes for any datatype. Also, generics generate compile time errors rather than run-time errors therefore it is useful for debugging processes. As an extension to the first advantage, users need not type cast variables as there are no pre-defined primitive types.
0 Comments