ch17 - Chapter Seventeen: Generic Programming Chapter Goals...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Seventeen: Generic Programming
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To understand the execution of generic methods in the virtual machine To know the limitations of generic programming in Java To understand the relationship between generic types and inheritance To learn how to constrain type variables
Background image of page 2
Type Variables Generic programming : creation of programming constructs that can be used with many different types In Java, achieved with inheritance or with type variables For example: Type variables: Java's ArrayList (e.g. ArrayList<String >) Inheritance: LinkedList implemented in Section 20.2 can store objects of any class Generic class : declared with a type variable E The type variable denotes the element type: public class ArrayList < E > // could use "ElementType" instead of E { public ArrayList() { . . . } public void add( E element) { . . . } . . . }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Type Variables Can be instantiated with class or interface types ArrayList<BankAccount > ArrayList<Measurable > Cannot use a primitive type as a type variable ArrayList<double> // Wrong ! Use corresponding wrapper class instead ArrayList<Double >
Background image of page 4
Type Variables Supplied type replaces type variable in class interface Example: add in ArrayList<BankAccount > has type variable E replaced with BankAccount: public void add( BankAccount element) Contrast with LinkedList.add: public void add(Object element)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Type Variables Increase Safety Type variables make generic code safer and easier to read Impossible to add a String into an ArrayList<BankAccount > Can add a String into a LinkedList intended to hold bank accounts ArrayList<BankAccount> accounts1 = new ArrayList<BankAccount>(); LinkedList accounts2 = new LinkedList(); // Should hold BankAccount objects accounts1.add("my savings"); // Compile-time error accounts2.add("my savings"); // Not detected at compile time . . . BankAccount account = (BankAccount) accounts2.getFirst(); // Run-time error
Background image of page 6
Syntax 22.1 Instantiating a Generic Class GenericClassName<Type1, Type2, . . .> Example: ArrayList<BankAccount> HashMap<String, Integer> Purpose: To supply specific types for the type variables of a generic class.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Self Check 17.1 The standard library provides a class HashMap<K, V> with key type K and value type V . Declare a hash map that maps strings to integers. Answer: HashMap<String, Integer>
Background image of page 8
The binary search tree class in Chapter 16 is an example of generic programming because you can use it with any classes that implement the Comparable interface. Does it achieve genericity through inheritance or type variables? Answer:
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.

Page1 / 47

ch17 - Chapter Seventeen: Generic Programming Chapter Goals...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online