CMSC 132: Object-Oriented Programming II. Generic Programming. Department of Computer Science University of Maryland, College Park

Similar documents
CMSC 132: Object-Oriented Programming II

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Generalized Code. Fall 2011 (Honors) 2

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CMSC 132: Object-Oriented Programming II

Java 5 New Language Features

Java: exceptions and genericity

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Enums, Generics, Nested Types. June 23, 2017

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

Photo credit: Andrew Kennedy JAVA GENERICS

Software Engineering. Prof. Agostino Poggi

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Computational Expression

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Introduction to Computer Science I

First Name Last Name ID#

1.1. Annotations History Lesson - C/C++

Parametric polymorphism and Generics

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CSE 8B Intro to CS: Java

Test Class A. public class A { private int x, y; public int getx() { return x; } public int gety() { return y; }

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

CS 211: Using ArrayList, Implementing Arraylist

ArrayLists. Chapter 12.1 in Savitch

JAVA V Source files Java, winter semester

Working with arrays. ArrayLists. Abstraction. Arrays

After the code has executed, what is the capacity of list? What is its size? c

CSE 331 Software Design & Implementation

ArrayLists. COMP1400 Week 8. Wednesday, 12 September 12

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

All answers will be posted on web site, and most will be reviewed in class.

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

Inf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics

Primitive Java Generic Class

Outline of lecture. i219 Software Design Methodology 6. Object oriented programming language 3. Kazuhiro Ogata (JAIST)

Using arrays to store data

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Java 1.5 in a Nutshell

Introduction to Generics. Defining a Class with a Type Parameter

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

Java Generics -- an introduction. Based on

01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?

CS 211: Using ArrayList, Implementing Arraylist

Generics method and class definitions which involve type parameters.

Computer Science 210 Data Structures Siena College Fall 2018

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

CITS1001 week 4 Grouping objects

Java Generics. Lecture CS1122 Summer 2008

Polymorphic (Generic) Programming in Java

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

PARAMETRIC POLYMORPHISM

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail)

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSE 331. Generics (Parametric Polymorphism)

Object Oriented Programming: Based on slides from Skrien Chapter 2

CMSC 202. Generics II

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

#Students per correct answers

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Rules and syntax for inheritance. The boring stuff

Introduction to Programming Using Java (98-388)

Unit5: Packages and abstraction. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

Generics/Templates. Steven R. Bagley

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CS 310: HW 1, Junit, Generics Review

CMSC 341. Nilanjan Banerjee

Improving structure with inheritance. Main concepts to be covered. The DoME example. DoME objects. DoME classes. DoME object model

JAVA V Assertions Java, winter semester

Properties of an identifier (and the object it represents) may be set at

Flowcharts [15 points]

Practice for Chapter 11

CS108, Stanford Handout #8. Java Generics

Collections, Maps and Generics

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Generalizing Collection Classes Using Generics with other Java 1.5 Features Integration of Generics with Previous Releases User built generic classes

CS Programming I: ArrayList

Section 10 MATERIAL PULLED FROM LAST SECTION AND LAST YEAR S SLIDES

Module 11. Collections and Iterators. Adapted from Absolute Java, Rose Williams, Binghamton University

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

The collections interfaces

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division DO NOT. P. N.

A Short Summary of Javali

Self-review Questions

Fall 2017 CISC124 9/16/2017

Transcription:

CMSC 132: Object-Oriented Programming II Generic Programming Department of Computer Science University of Maryland, College Park

Generic Programming Generic programming Defining constructs that can be used with different data types I.e., using same code for different data types Example: stack operations the same regardless of stack element type Implemented in Java through Inheritance A extends B Type variables <A>

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 ); dowork( ) applied to objects of both class A and B 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 );

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>

Defining a Generic Class Example public class mygeneric<t> { private T value; public mygeneric( T v ) { value = v; } public T getval( ) { return value; } } Append type variable(s) to class name using angle brackets ClassName<type variable> Can use any name for type variable (but typically single uppercase letter E, K, V, etc ) http://docs.oracle.com/javase/7/docs/api/java/util/map.html Use the type variable to define type of variables, type of method parameters, method return type and object allocation Arrays Type of an array object may not be a type variable or a parameterized type, unless it is an unbounded wildcard type How to define arrays? T[] data = (T[]) new Object[size]; Example: Queue.java

Generics and Subtyping In general if B is a subtype of A, and GT is a generic type declaration, it is not the case that GT<B> is a subtype of GT<A> Example ArrayList<String> strl = new ArrayList<String>(); ArrayList<Object> objl = strl; // Illegal!

Generics and Subtyping Consider what could happen if legal class A { } class B extends A { } // B is subtype of A List<B> bl = new ArrayList<B>(); List<A> al = bl; al.add(new A()); B b = bl.get(0); // runtime exception Using String Class ArrayList<String> sl = new ArrayList<String>(); ArrayList<Object> ol = sl; // Illegal, but let s assume is valid ol.add(new Integer(10)); String entry = sl.get(0); // Problem!!

Subtyping and Arrays Subtyping works for arrays class A { } class B extends A { } // B is subtype of A A a = new B(); // B can be used where A expected B[] bb = new B[1]; A[] ab = bb; bb[0] = a; // won't compile Using String Class Object value = new String("HI"); String[] ss = new String[1]; Object[] oo = ss; // Legal ss[0] = value; // It will not Compile Example: Fruit.java, TropicalFruit.java

Wildcards? (unknown) Collection<?> Bounded Wildcard Collection whose element type matches anything Example: ArrayList<? extends Shape> Summary Unknown type that is Shape or subtype of Shape Notice the meaning of extends in this context <?> unknown type <? extends typeexpression> unknown type that is typeexpression or a subtype of typeexpression <? super typeexpression> unknown type that is typeexpression or a supertype of typeexpression. typeexpression can involve further occurrences of wildcard type expressions Example: WildCard.java