Generics.

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

Generics and The ArrayList Class
Generic programming in Java
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Abstract Classes and Interfaces
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapters (ArrayList / Generic)
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to Generics
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Java Generics.
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Generics, Exceptions and Undo Command
Classes and Objects.
Computer Engineering Department Islamic University of Gaza
ADT’s, Collections/Generics and Iterators
Chapter 19 Generics Dr. Clincy - Lecture.
Exceptions Handling the unexpected
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Lecture 22 Inheritance Richard Gesick.
Writing Methods AP Computer Science A.
Polymorphism and access control
Sets, Maps and Hash Tables
Generic programming in Java
Chapter 18 – Generic Classes
Advanced Programming in Java
Java – Inheritance.
Unit 3 Test: Friday.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Advanced Programming in Java
Stacks.
CMSC 202 Generics.
Fundaments of Game Design
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Review of Previous Lesson
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 19 Generics.
Subtype Substitution Principle
Templates An introduction.
Creating and Using Classes
Generics, Lambdas and Reflection
Presentation transcript:

Generics

Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able to store elements of a certain type in a container, say an ArrayList This ability does not pose any special requirements on the type itself RHS – SWC

Generics public class ArrayList { public ArrayList() {…} public void add(Object e) {…} } RHS – SWC

Generics Since all classes inherit from Object, this will work – in principle However, there is nothing to prevent us from storing elements of different types in the same ArrayList Will complicate processing of the elements Risk for errors at run-time RHS – SWC

Generics A better (safer) solution is to use Generics In a generic class, one or more type parameters are included in the definition Example is the well-known ArrayList<E> RHS – SWC

Generics public class ArrayList<E> { public ArrayList() {…} public void add(E e) {…} } E is the type parameter RHS – SWC

Generics When we wish to use the class, we must ”instantiate” it with a concrete type: ArrayList<BankAccount> ArrayList<Car> We can use the ArrayList for all types, BUT we cannot put two objects of different types into the same ArrayList RHS – SWC

Creating a generic class Creating a generic class is almost like creating an ordinary class However, one or more of the types are turned into type parameters Consider a class for storing a pair of objects, which may have different types Pair<String, Car> RHS – SWC

Creating a generic class public class Pair<T,S> { private T first; private S second public Pair(T first, S Second) {...} public T getFirst() {return first;} public S getSecond() {return second;} } RHS – SWC

Creating a generic method An ordinary method inside a class usually does not need a type parameter Type parameter for class is used However, this is not the case for static methods: public static <E> void print(E[] a) RHS – SWC

Creating a generic method When invoking a generic method, we do not need to specify a type parameter: Car[] carList; ArrayUtil.print(carList); The compiler can figure out what the concrete type of the parameter is RHS – SWC

Type constraints Sometimes, we will only allow types that fulfill some properties Consider the min method: public static <E> E min(E a, E b) { if (a < b) return a; else return b; } RHS – SWC

Type constraints The previous code will only work for types that can be compared Type must implement the Comparable interface We must constrain the type parameter to fulfill this property RHS – SWC

Type constraints public static <E extends Comparable> E min(E a, E b) { if (a < b) return a; else return b; } RHS – SWC

Under the hood How does the Java Virtual Machine handle generics…? It substitutes the type variables with a concrete type, depending on the type constraints RHS – SWC

Under the hood public class Pair<T, S> { private T first; private S second public Pair(T first, S Second) {...} public T getFirst() {return first;} public S getSecond() {return second;} } RHS – SWC

Under the hood public class Pair { private Object first; private Object second public Pair(Object first, Object Second) {...} public Object getFirst() {return first;} public Object getSecond() {return second;} } RHS – SWC

Under the hood But will that always work…? Yes, because the compiler has checked that all generic types are used correctly It does however impose some limitations on the code in generic methods: E e = new E(); This is illegal…(why)? RHS – SWC

Under the hood Why does it work like that? Enables pre-generics code to work together with code using generics Does it matter in practice? Not much, but it explains why some perhaps not-so-obvious limitations exist RHS – SWC

Exercises Review: R16.5, R16.7, R16.8 Programming P16.1, P16.2 RHS – SWC