Generic programming in Java


Anshusharma1169

Uploaded on Sep 18, 2020

Category Education

www.acem.edu.in

Category Education

Comments

                     

Generic programming in Java

Object Oriented Using Java - Generic in Java PRESENTED BY: ANSHU SHARMA GENERICS IN JAVA  Generics in Java is similar to  templates in C++. The idea is to allow type (Integer, String, … etc and user defined types) to be a parameter to methods, classes and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well. We can use them for any type. GENERIC CLASS  we use to specify parameter types in generic class creation. To create objects of generic class, we use following syntax. // To create an instance of generic class BaseType obj = new BaseType () Note: In Parameter type we can not use primitives like 'int','char' or 'double'. EXAMPLES: GENERIC CLASS / A Simple Java program to show working of user defined Generic classes // We use < > to specify Parameter type class Test {     // An object of type T is declared     T obj;     Test(T obj) {  this.obj = obj;  }  // constructor      public T getObject()  { return this.obj; } }     EXAMPLE CONTD. class Main {     public static void main (String[] args)     {         // instance of Integer type         Test iObj = new Test(15);         System.out.println(iObj.getObject());             // instance of String type         Test sObj =                           new Test("GeeksForGeeks");         System.out.println(sObj.getObject());     } } MULTIPLE TYPE PARAMETERS IN GENERIC CLASSES We can also pass multiple Type parameters in Generic classes. // A Simple Java program to show multiple // type parameters in Java Generics    // We use < > to specify Parameter type class Test {     T obj1;  // An object of type T     U obj2;  // An object of type U        // constructor     Test(T obj1, U obj2)     {         this.obj1 = obj1;         this.obj2 = obj2;     }    EXAMPLE CONTD. // To print objects of T and U     public void print()     {         System.out.println(obj1);         System.out.println(obj2);     } }    // Driver class to test above class Main {     public static void main (String[] args)     {         Test obj =             new Test("GfG", 15);            obj.print();     } } GENERIC FUNCTIONS:  We can also write generic functions that can be called with different types of arguments based on the type of arguments passed to generic method, the compiler handles each method. EXAMPLE: // A Simple Java program to show working of user defined // Generic functions     class Test {     // A Generic method example     static void genericDisplay (T element)     {         System.out.println(element.getClass().getName() +                            " = " + element);     }        EXAMPLE CONTD. // Driver method    public static void main(String[] args)     {          // Calling generic method with Integer argument         genericDisplay(11);             // Calling generic method with String argument         genericDisplay("GeeksForGeeks");             // Calling generic method with double argument         genericDisplay(1.0);     } } ADVANTAGES OF JAVA GENERICS 1. Code Reusability  Generics allow us to write code that will work with different types of data. For example, public void genericsMethod(T data) {...} Here, we have created a generics method. This method can be used to perform operations on integer data, string data and so on. ADVANTAGES CONTD. 2. Compile-time Type Checking  The type parameter of generics provides information about the type of data used in the generics code.  Hence, any error can be identified at compile time which is easier to fix than runtime errors. For example,  // without using Generics NormalClass list = new NormalClass(); // calls method of NormalClass list.display("String"); In the above code, we have a normal class. We call the method named display() of this class by passing a string data.  Here, the compiler does not know if the value passed in the argument is correct or not. ADVANTAGES CONTD.  However, let's see what will happen if we use the generics class instead.  // using Generics GenericsClass list = new GenericsClass(); // calls method of GenericsClass list2.display("String"); In the above code, we have a generics class. Here, the type parameter indicates that the class is working on Integer data. Hence when the string data is passed in argument, the compiler will generate an error.