Definition § Generics or parameterized types. § The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. § Using Generics, it is possible to create classes that work with different data types. § An entity such as class, interface, or method that operates on a parameterized type is called a generic entity.
Case 1 class Test<T> { T obj; Test(T obj) { obj = obj; } public T getObject() { return obj; } } class Main { public static void main(String[] args) { Test<Integer> iObj = new Test<Integer>(15); System.out.println(iObj.getObject()); Test<String> sObj = new Test<String>("Hello World!"); System.out.println(sObj.getObject()); } } // we can not use primitives like int, char or double.
Case 3 class Test<T, U> { T obj1; // An object of type T U obj2; // An object of type U Test(T obj1, U obj2) { this.obj1 = obj1; this.obj2 = obj2; } public void print() { System.out.println(obj1); System.out.println(obj2); } } class Main { public static void main (String[] args) { Test <String, Integer> obj = new Test<String, Integer>(”ABC", 15); obj.print(); }
Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.