Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…

Similar presentations


Presentation on theme: "Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…"— Presentation transcript:

1 Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff… We have covered what you need to know for assn 4 – due March 23. Next quiz in two weeks. (Marking underway for Quiz 2). Exercise 9 would be good preparation for assn 4. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Today Generic Bounding and Wildcards. The Class<T> Object.
Generic Factory using Reflection. Winter 2018 CMPE212 - Prof. McLeod

3 Generics in Modern Java
This discussion assumes newer versions of Java (≥ 1.8) Generics allow you to write powerful, compact code that can work with many object types. Particularly useful with hierarchies. Type inference, bounding, wildcards and the use of the Class<T> object adds power and convenience. Let’s experiment! Winter 2018 CMPE212 - Prof. McLeod

4 First Demo TestWildcards.java
Uses the Person hierarchy from before. Now, the constructors throw exceptions, just for fun. Plays with wildcards, bounded generic methods, and how to create an array of generic classes. Winter 2018 CMPE212 - Prof. McLeod

5 Summary: Wildcards If Child is a subclass of Parent then this works:
Parent obj = new Child(); But this will not work: ArrayList<Parent> alObj = ArrayList<Child>(); This will work: ArrayList<?> alObj = ArrayList<Child>(); A “wildcard” is the ? within the < >. A wildcard can provide a super type generic class for other generic classes. Winter 2018 CMPE212 - Prof. McLeod

6 Summary: Wildcards, Cont.
You can bind a wildcard to classes that extend other classes using the extends keyword. For example: ArrayList<? extends Person> db db can be assigned to an object of type ArrayList<Person>, ArrayList<Student> or ArrayList<Professor>. Winter 2018 CMPE212 - Prof. McLeod

7 Summary: Wildcards, Cont.
However, a generic typed with a wildcard is not mutable. You can get around this using generic methods without the wildcard. But: This is more restrictive and potentially “brittle”. Avoid casting using the generic type. This may lead to “Unchecked Casts” which reduces the type safety of the generic method. (Hint: Use of Class<T> may be a way around this problem. Winter 2018 CMPE212 - Prof. McLeod

8 Summary: Wildcards, Cont.
ArrayList<?>[] is the only possible type that allows the creation of an array of generic classes. But, why would you want to? Easier to create an ArrayList of ArrayLists, for example. Winter 2018 CMPE212 - Prof. McLeod

9 Aside – Lower Bound for Wildcard
In ? extends T the extends keyword represents an upper bound for the type – T or any sub-type of T. You can also use super as in ? super T which provides a lower bound for the type – T or any super-type of T. Winter 2018 CMPE212 - Prof. McLeod

10 Second Demo GenericFactoryDemo.java
Can a generic method instantiate an array of type T? Can a generic method instantiate a generic class typed to T? Can a generic method instantiate objects of type T at all? If so, how? Winter 2018 CMPE212 - Prof. McLeod

11 Summary: Generic Factories
Factory methods generate objects, or more specifically, collections of objects. How can you make these methods non- type-specific? You cannot create an array of type T in a generic method unless you can tolerate an unchecked cast to T[]. You cannot instantiate objects of type T in a generic method directly. Winter 2018 CMPE212 - Prof. McLeod

12 The Class<T> Object
You can pass a type into a method using a Class<T> object. The Object class contains a method .getClass() that returns a Class<T> object. Or you can just use the .class literal on a type. For example: String aString = "Hello!"; System.out.println(aString.getClass()); System.out.println(String.class); Shows: class java.lang.String Winter 2018 CMPE212 - Prof. McLeod

13 The Class<T> Object, Cont.
See the API for lots of methods belonging to this Object. Useful for determining all kinds of information about a type or an instance. You used to be able to invoke .newInstance() from a Class<T> object and this would create a new instance of T by invoking the default constructor. You would then need to use mutators to assign attributes. Clumsy!! This method is now depreciated. Winter 2018 CMPE212 - Prof. McLeod

14 The Class<T> Object, Cont.
Instead, invoke .getConstructor(paramTypes) on the Class<T> object. paramTypes is an array of Class<?> objects obtained using the .class attribute that matches the parameter list for the constructor you want to use. This method returns a Constructor<T> object, from which you can invoke .newInstance(params). params is an array of type Object[] containing the actual arguments for the constructor. Winter 2018 CMPE212 - Prof. McLeod

15 Reflection You are constructing an instance without using new!
This technique is called “Reflection”. Type Introspection is the process of discovering class structure at runtime (methods, annotations, attributes, inner classes, etc.) through a Class<T> object. Even if they are private! Reflection is what happens when you use this information at run-time. Winter 2018 CMPE212 - Prof. McLeod

16 Reflection, Cont. For example, JUnit testing uses this technique to discover and then run as tests all methods annotated with Problems include performance hits, as well as security and exposure issues. See: Winter 2018 CMPE212 - Prof. McLeod


Download ppt "Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…"

Similar presentations


Ads by Google