13Generics

13Generics - CMSC 132 Object-Oriented Programming II...

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

View Full Document Right Arrow Icon
CMSC 132: Object-Oriented Programming II Generic Programming Department of Computer Science University of Maryland, College Park
Background image of page 1

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

View Full DocumentRight Arrow Icon
Generic Programming Generic programming Defining constructs that can be used with different data types I.e., using same code for different data types Implemented in Java through 1. Inheritance A extends B 2. Type variables <A>
Background image of page 2
Generic Programming Examples Inheritance Class A { doWork( A x ) { … } } Class B extends A { … } A w1 = new A( ); B w2 = new B( ); w1.doWork( w1 ); w2.doWork( w2 ); Type Variables Class W <T> { doWork( T x ) { … } } Class A { … } Class B { … } W<A> x1 = new W<A>( ); W<B> x2 = new W<B>( ); A w1 = new A( ); B w2 = new B( ); x1.doWork( w1 ); x2.doWork( w2 ); doWork( ) applied to objects of both class A and B
Background image of page 3

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

View Full DocumentRight Arrow Icon
Generic Class Class with one or more type variables Example class ArrayList<E> To use generic class, provide an actual type Valid types Class ArrayList< String > Interface ArrayList< Comparable > Invalid types Primitive type ArrayList< int > (use wrappers) ArrayList< Integer >
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2011 for the course CMSC 132 taught by Professor Padua-perez during the Spring '08 term at Maryland.

Page1 / 10

13Generics - CMSC 132 Object-Oriented Programming II...

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

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