Generics in Java

To be able to pass different data types such as Integer, String, user-defined data types, and so on as a parameter to methods, interfaces, classes, we make use of a parameterized type in Java called Generics using which classes can be created. These are capable of working with different data types and any entity like class, interface, or method operating on this parameterized type or Generics is called a generic entity.

Syntax to Create Objects of Generic Class

The syntax to create objects of Generic class is as follows:

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

generic_class_name<type>object_name = new generic_class_name <type>()

Where generic_class_name is the name of the generic class, type is the data type such as Integer, String, user-defined data types, etc., passed as a parameter to the generic class and new is the keyword used to create an object or instance of the generic class.

The necessity to use Generics in Java is as follows:

  • The type safety feature is provided by using Generics in Java. Whenever the errors in the program are reported at compile time and not at runtime, then it is called Type Safety. By making use of Generics, such errors are pointed out at compile-time only.
  • The functionalities provided by the templates in C++ are also provided by Generics in Java.
  • Generics in Java are used in HashSet classes, ArryList classes, HashMap classes, etc as well.
  • It can pass multiple type parameters as a parameter to generic classes.
  • Generics in Java supports code reusability. The method or class or interface can be written once and can be used for any type we want to.
  • By using Generics in Java, individual typecasting is not necessary while working with Array Lists.
  • Algorithms working on different types of objects that are type safe can be implemented using Generics in Java.

The working of Generic class is as follows:

  • A class operating on a parametrized type or Generics is called a generic class.
  • The syntax to create objects of Generic class is as follows:

generic_class_name<type>object_name = new generic_class_name <type>()

Where generic_class_name is the name of the generic class,

type is the data type such as Integer, String, user-defined data types, etc., passed as a parameter to the generic class and new is the keyword used to create an object or instance of the generic class.

  • The primitive data types like int, char, or double cannot be passed as a parameter type to create an instance of the generic class.
  • Multiple type parameters can be passed as parameters to the generic classes.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Examples Of Generics in Java

Example 1: Java program to demonstrate the creation of generic class by passing a parameter to the generic class, defining a method in the generic class and then creating instances of the generic class and then to call the method of the generic class to display a character and to display a string as the output on the screen:

//a generic class called check is created which takes the type as a parameter

class check<parametertype> 

     //an object of type passed as parameter is created

     parametertype object_name; 

     //constructor of the generic class is created

     check(parametertype object_name) 

     {  

         this.object_name = object_name;  

    } 

    //a method of generic class to return the object is created

    public parametertype obtainObject()  

    { 

         return this.object_name;  

    } 

}

//main class is defined within which it creates the instances of the generic class

class Main 

     //main method is called 

     public static void main (String[] args) 

     { 

         //an instance of the generic class check is created to display a character 'A'

         check <Character> object_name1 = new check<Character>('A'); 

System.out.println("The character displayed by creating an instance of check generic class is:\n");

         System.out.println(object_name1.obtainObject());  

         //an instance of the generic class check is created to display a string

check <String> object_name2 = new check<String>("Welcome to Simplilearn"); 

System.out.println("\nThe string displayed by creating an instance of check generic class is:\n");

         System.out.println(object_name2.obtainObject()); 

     } 

}

It shows the output of the above program in the snapshot below:

generics-java

In the above program, we are creating a generic class called check which takes ‘type’ as the parameter. Then an object of ‘type’ passed as a parameter to the generic class check is created. Then the constructor of the generic class is created. Then the method of the generic class checks to return the object is created. Then, the main method is called within which the instances of the generic class are created. Then two instances of the generic class are created to display the output on the screen. The output is shown in the snapshot above.

Working of Generic Function

The working of Generic function is as follows:

  • Like generic classes, generic functions can also be defined by passing different parameters to the generic functions.
  • The compiler handles each generic function.
  • The syntax to create objects of Generic function is as follows:

<type>generic_function_name(parametertype parameter)

{

Body of the function

}

where generic_function_name is the name of the generic function and

type is the data type such as Integer, String, user-defined data types., passed as a parameter to the generic function.

Example 2: Java program to demonstrate the creation of generic class by passing a parameter to the generic class, defining a method or function in the generic class and then creating instances of the generic class and then to call the method of the generic class to display the output on the screen:

//a generic class called check is created which takes the type as the parameter

class check<parametertype> 

     //a method of generic class to display the output is defined

     public <parametertype> void display(parametertype parameter)  

     { 

System.out.println("\nThe string displayed by making use of generic function is:\n");

         System.out.println("Hello, " + parameter);  

     } 

}

//main class is defined within which the main method is called 

public class Main

{

     //main method is called 

     public static void main (String[] args) 

     { 

//an instance of the check class is created

         check object_name = new check();

//the generic function display of the generic class is called to display the output on the screen

         object_name.display("Welcome to Simplilearn"); 

        } 

}

It shows the output of the above program in the snapshot below:

welcome-simpli-generics

In the above program, we are creating a generic class called ‘check’ which takes ‘type’ as the parameter. Then the method or function of the generic class check to concatenate the given two statements is created. We call the main method within which it creates the instance of the generic class to which a string is passed, which then is concatenated with the string in the generic class to display as the output on the screen. The output is shown in the snapshot above.

The advantages of Generics in Java is as follows:

  • The type-safety feature is provided by using Generics in Java. By making use of Generics, we point such type-safe errors out at compile-time only.
  • Generics in Java supports code reusability. The method or class or interface can be written once and can be used for any type we want to.
  • By using Generics in Java, individual typecasting is not necessary while working with Array Lists.
Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

In this article, we have learned the concept of generics in Java, the need for generics, the working of generic class and generic functions along the advantages of using generics in Java. You can also strengthen your Java skills with our Java Certification Training Program, starting now!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.