1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Generics and the ArrayList Class
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Generics and The ArrayList Class
Sadegh Aliakbary Sharif University of Technology Fall 2012.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Java Generics.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
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.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
 2006 Pearson Education, Inc. All rights reserved Generics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
OOP Languages: Java vs C++
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Effective Java: Generics Last Updated: Spring 2009.
 2005 Pearson Education, Inc. All rights reserved Generics.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Understanding Data Types and Collections Lesson 2.
Introduction to Generics
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
CIS 270—Application Development II Chapter 18-Generics.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Peyman Dodangeh Sharif University of Technology Spring 2014.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CMSC 330: Organization of Programming Languages Java Generics.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Learners Support Publications Constructors and Destructors.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Java Generics.
Sections Inheritance and Abstract Classes
Generic(Parameterized ) Types
Chapter 20 Generic Classes and Methods
Chapter 14 Templates C++ How to Program, 8/e
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics, Lambdas, Reflections
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Generics.
Constructors and Destructors
Generics, Lambdas, Reflections
Chapter 19 Generics.
Generic Classes and Methods
Presentation transcript:

1 L40 Generics (2)

2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information about a parameter is not required in the method body.  The relationship between generics and inheritance.

Generic Classes Generic classes – Use a simple, concise notation to indicate the actual type(s) – At compilation time, Java compiler ensures the type safety uses the erasure technique to enable client code to interact with the generic class Parameterized classes – Also called parameterized types – E.g., Stack

Generic Classes (Cont.) Generic class declaration – Looks like a non-generic class declaration – Except class name is followed by a type parameter section The –Xlint:unchecked option – Compiler cannot 100% ensure type safety

5 Outline Stack.java (1 of 2) Line 4 Line 8 Line 22 Generic class declaration, class name is followed by a type parameter section Declare elements as an array that stores objects of type E Create an array of type E. The generic mechanism does not allow type parameter in array-creation expressions because the type parameter is not available at runtime

6 Outline Stack.java (2 of 2) Lines Lines Method push pushes element of type E onto stack Method pop returns the top element, which is of type E

7 Outline FullStack Exception.java

8 Outline EmptyStack Exception.java

Generic Classes (Cont.) Generic class at compilation time – Compiler performs erasure on class’s type parameters – Compiler replaces type parameters with their upper bound Generic class test program at compilation time – Compiler performs type checking – Compiler inserts cast operations as necessary

10 Outline StackTest.java (1 of 6) Line 9 Line 10 Lines Generic class Stack ’s type argument is Double Generic class Stack ’s type argument is Integer Instantiate object doubleStack of size 5 and ingeterStack of size 10

11 Outline StackTest.java (2 of 6) Line 36 Invoke Stack ’s method push to place a double value onto doubleStack

12 Outline StackTest.java (3 of 6) Line 58 Auto-unboxing occurs when the value returned by pop ( Double ) is assigned to a double primitive variable

13 Outline StackTest.java (4 of 6) Line 81 Invoke Stack ’s method push to place an int value onto integerStack

14 Outline StackTest.java (5 of 6) Line 103 Auto-unboxing occurs when the value returned by pop ( Integer ) is assigned to an int primitive variable

15 Outline StackTest.java (6 of 6) Program output

Generic Classes (Cont.) Creating generic methods to test class Stack – Method testPush Perform same tasks as testPushDouble and testPushInteger – Method testPop Perform same tasks as testPopDouble and testPopInteger

17 Outline StackTest2.java (1 of 4) Lines Invoke generic methods testPush and testPop to push elements onto stack and pop elements from stack

18 Outline StackTest2.java (2 of 4) Lines Line 35 Generic method testPush replaces testPushDouble and testPushInteger Replace element type Double/Integer with type parameter T

19 Outline StackTest2.java (3 of 4) Lines Line 55 Generic method testPop replaces testPopDouble and testPopInteger Replace element type Double/Integer with type parameter T

20 Outline StackTest2.java (4 of 4) Program output

Raw Types Raw type – Enables to instantiate generic class without specifying a type argument e.g., Stack objectStack = new Stack( 5 ); objectStack is said to have a raw type – Important for backwards compatibility with prior versions – A raw type Stack variable can be assigned a Stack that specifies a type argument – A Stack variable that specifies a type argument can be assigned a raw type Stack Permitted but unsafe Use the –Xlint:unchecked option to compile

22 Outline RawTypeTest.java (1 of 5) Line 14 Line 17 Line 20 Instantiate generic class Stack with raw type Assign a Stack to variable rawTypeStack2 Assign a Stack of raw type to Stack. Legal but unsafe

23 Outline RawTypeTest.java (2 of 5)

24 Outline RawTypeTest.java (3 of 5)

25 Outline RawTypeTest.java (4 of 5) Program output

26 Outline RawTypeTest.java (5 of 5) Program output

27 Outline Fig | Warning message from the compiler.

Wildcards in Methods That Accept Type Parameters Data structure ArrayList – Dynamically resizable, array-like data structure – Method add – Method toString Motivation for using wildcards – Implement a generic method sum Total the numbers in a collection Receive a parameter of type ArrayList Use method doubleValue of class Number to obtain the Number ’s underlying primitive value as a double value

29 Outline TotalNumbers.java (1 of 2) Line 11 Line 12 Line 15 Line 19 Declare and initialize array numbers Declare and initialize numberList, which stores Number objects Add elements in numbers array to ArrayList numberList Invoke method sum to calculate the total of the elements stored in numberList

30 Outline TotalNumbers.java (2 of 2) Line 23 Lines Program output Method sum accepts an ArrayList that stores Number objects Use method doubleValue of class Number to obtain the Number ’s underlying primitive value as a double value

Wildcards in Methods That Accept Type Parameters (Cont.) Implementing method sum with a wildcard type argument in its parameter – Number is the superclass of Integer – ArrayList is not a supertype of ArrayList – Cannot pass ArrayList to method sum – Use wildcard to create a more flexible version of sum ArrayList ? Represents an “unknown type” Unknown type argument must be either Number or a subclass of Number Cannot use wildcard as a type name through method body

32 Outline WildcardTest.java (1 of 3) Line 12 Line 20 Line 25 Declare and create ArrayList integerList to hold Integer s Invoke method sum to calculate the total of the elements stored in integerList Declare and create ArrayList doubleList to hold Double s

33 Outline WildcardTest.java (2 of 3) Line 33 Line 38 Line 46 Line 50 Invoke method sum to calculate the total of the elements stored in doubleList Invoke method sum to calculate the total of the elements stored in numberList Declare and create ArrayList integerList to hold Numbers s The ArrayList argument’s element types are not directly known by the method, they are known to be at least of type Number

34 Outline WildcardTest.java (3 of 3) Program output

Generics and Inheritance: Notes Inheritance in generics – Generic class can be derived from non-generic class e.g., class Object is superclass of every generic class – Generic class can be derived from another generic class e.g., Stack is a subclass of Vector – Non-generic class can be derived from generic class e.g., Properties is a subclass of Hashtable – Generic method in subclass can override generic method in superclass If both methods have the same signature