Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 1 Chapter 17 Animated Version Generics and Type Safety.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 1 Chapter 17 Animated Version Generics and Type Safety."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 1 Chapter 17 Animated Version Generics and Type Safety

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 2 Chapter 17 Objectives After you have read and studied this chapter, you should be able to –Define generic classes –Define classes by using generics, inheritance, and interfaces –Instantiate type-safe Java Collection Framework classes –Describe how the generics mechanism supports type safety –Define a simple linked list using generics

3 Introduction In Chapter 10, we saw a simple example of generics when defining a homogeneous collection. The generics mechanism adds much needed type safety in Java when dealing with collections of objects. In this chapter, we provide a detailed description of generics in Java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 3

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 4 A Simple Example: Magical Locker class Locker { private T content; public Locker( ) { store(null); } public T retrieve( ) { return content; } public void store(T item) { content = item; } Generic ClassUsage //Declaration Locker lockerOne; Locker lockerTwo; //Creation lockerOne = new Locker (); lockerTwo = new Locker (); //Usage lockerOne.store(“Hello”); lockerTwo.store(new Integer(10));...

5 Generics Illustrated ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 5

6 Multiple Type Parameters A generic class can be specified with multiple type parameters T1 and T2 can be of any type ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 6 class SecondLocker { private T1 content1; private T2 content2;... public T1 retrieveFirstItem( ) { return content1; }... public T2 retrieveSecondItem( ) { return content2; }... } SecondLocker locker2;

7 Generics Support Type Safety Without generic classes, we must always face the danger of runtime error. For example: ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 7 ArrayList list = new ArrayList(); list.add(“Hello”); Integer intObj = (Integer) list.get(0); //Runtime type cast error By specifying the pluggable type, we can detect such error at the compile time ArrayList list;... list = new ArrayList ; //Compilation error

8 Bounded Types With the Locker generic class, we can plug in any class its type parameter. We can restrict the classes that can be specified the type parameter. Restricted type parameters are called bounded types. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 8 class FourthLocker {... } FourthLocker one; FourthLocker two; FourthLocker three; FourthLocker nogood; Valid NOT Valid

9 Wildcard Types The question mark symbol ? designates the wildcard type that allows any type ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 9 class FifthLocker {... public boolean isSameValue(FifthLocker item) { return this.retrieve().doubleValue() == item.retrieve().doubleValue(); } This method declaration will allow us to pass an item of any valid FifthLocker concrete type.

10 Restrictions You cannot create an instance of the type parameter inside a generic class Static data members cannot be declared as the type parameter T The return type of static methods cannot be declared as the type parameter T ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 10

11 Generics and Collections As seen in Chapter 10, generics support the creation of homogeneous collections Without generics, when we call methods of an element in a collection, we must typecast the element (even if all elements in the collection is of the same type) We use the same technique discussed so far to define our own generic collection classes –See Node and SimpleLinkedList ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 11

12 A Subclass of a Generic Superclass When defining a subclass of a generic superclass, we must include the type parameter of the superclass in the subclass public class LockerSub extends Locker { … } The subclass can include additional type parameter public class LockerSubTwo extends Locker { … } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 12

13 Generic Interfaces In addition to generic classes, we can define generic interfaces interface MagicContainer { … } Similar to creating a subclass of a generic superclass, a class that implements a generic interface must include the type parameter of the interface class MagicTrunk implements MagicContainter {…} –Type paramenter in the class can be bounded class MagicTrunk implements MagicContainer { … } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 13

14 Raw Types The following statements do not cause an error ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 14 Locker locker = new Locker(); //Locker is a generic class locker.store(new Integer(4)); Why don’t we have to declare locker to be of type Locker ? If we don’t specify the concrete type for the type parameter when creating an instance of a generic class, a raw type is created The raw type is dangerous and sabotages the type safety benefit of generics The raw type is allowed simply to maintain the backward compatibility. Avoid using raw types.

15 Generics and Arrays An array of type-specified generic class is not allowed Locker [ ] lockers; lockers = new Locker [25]; //INVALID An array of raw type generic class or a wildcard generic class is allowed Locker[ ] rawLockers = new Locker[25]; //OKAY Locker [ ] wildLockers = new Locker [25]; //OKAY The best approach is not to use arrays with generics. Using an ArrayList or a LinkedList instead. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 15


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 1 Chapter 17 Animated Version Generics and Type Safety."

Similar presentations


Ads by Google