chapter 21 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 21 PowerPoint Presentation

Chapter 21

118 Views Download Presentation
Download Presentation

Chapter 21

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 21 Generics

  2. Motivation • Many algorithms, containers, and methods can be implemented the same way independent of the data processed • The code can be written to process superclass types, or even the Object type, but a lot of casting and type checking is required • Generics allow writing a standard implementation that can handle any type

  3. Implementation • Classes, methods and interfaces can be defined using generics • Generics are declared with one or more placeholder variables that get replaced with a specific type when the class is instantiated • The specific types must be reference types, not primitive types • Placeholders are typically <T> or <E>

  4. Example: ArrayList • Old style ArrayList contained Object, which had to be cast to the real class contained • New generic implementation is ArrayList<T>, where T is replaced with the type we want to store • Out concrete ArrayList will only store the class type we specify, and the methods will seamlessly process that specific type

  5. Defining a generic class • Declaring (or instantiating) the generic requires adding the <T> after the class name: public class MyGeneric<T> • Write methods and declare variables, return values, and parameters using T in place of the actual type wherever the generic type will be used:protected T getValue();

  6. Generic Constructors • Variables are defined using the ClassName<T> syntax • Constructors are called using the ClassName<T>()syntax (note parentheses) • Constructors are defined the same way as ordinary classes: ClassName() • myG<Double> g = new myG<Double>();

  7. Generic Variables and Return Types • The general rule is to use T everywhere you need to declare a value of the generic type • Generic variables are declared using just T as a placeholder: T myVal; • Generic return values are declared using just T as a placeholder: public T getVal(); • Generic parameters are declared using just T as a placeholder: private void doWork(T item);

  8. GenericStack implementation

  9. Using the GenericStack

  10. Generic Methods • Generic methods can handle a variety of types • The type of data that the method will handle is defined when a particular instantiation of the method is created • Multiple versions of the method can be created to handle different data types • Only one versions of the generic method needs to be written and maintained

  11. Limiting Generic Types • A generic type can extend a supertype to limit the types it can take* • The syntax is <T extends supertype> • Generic types defined this way are bounded

  12. Backward Compatibility • Generic classes can be used without specifying a type • This is equivalent to defining the generic class using <Object> • A class specified this way is a Raw Type • It is for backward compatibility and should not be used for writing new code

  13. Wildcard Generic Types • Suppose we define a method that takes a generic class type as a parameter: void method(GenericClass<T> parm) • Each version of the generic class is distinct from every other version, resulting in ambiguity and a compiler error • How can we specify that different versions of the generic class are acceptable as parameters?

  14. The ? And Generics • Generic class parameters can be specified with a ? indicating that the type is unknown • GenericClass<?> means any type is acceptable • GenericClass<? extends ClassType> means ClassType or any of its subclasses is acceptable • GenericClass<? super T> means type T or any of its ancestor (supertype) classes is acceptable

  15. The Generic Stack Class Redux

  16. Creating a wildcard generic method • We want to pass a GenericStack of numbers to a method that will find the maximum value • The base class Number is the logical choice for handling whole and real numbers

  17. Using the wildcard generic method

  18. Notes on ? generics • <? extends ClassName> cannot be used with classes declared final (final classes cannot be extended) • Generic information is stripped out of the runtime code and replaced by Raw Types, Object, or bounded type references (erasure) • One type-less version of the class exists, so all instances compare true using instanceof

  19. Restrictions on Generics • The construct T obj = new T(); is illegal • The construct T[] array = new T[count]; is also illegal • Trying ArrayList<String>[] listarray = new ArrayList<String>[size]; is illegal • Generic types in static contexts is illegal:public static void method(T parm) public static T var; • Generics may not extend java.lang.Throwable (no generic exception classes): public class GenEx<T> extends Exception