©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 17 - 1 Chapter 17 Animated Version Generics and Type Safety.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Generics and The ArrayList Class
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
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.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Generics.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Introduction to Generics
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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.
(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.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 20 Generic Classes and Methods
Chapter 19 Generics Dr. Clincy - Lecture.
Generics.
Chapter 19 Generics.
Chapter 21 Generics.
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Review: libraries and packages
Chapter 21 Generics.
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Objectives After you have read and studied this chapter, you should be able to –Define generic classes –Define classes by using generics, inheritance, and interfaces –Instantiate type-safe Java Collection Framework classes –Describe how the generics mechanism supports type safety –Define a simple linked list using generics

Introduction In Chapter 10, we saw a simple example of generics when defining a homogeneous collection. The generics mechanism adds much needed type safety in Java when dealing with collections of objects. In this chapter, we provide a detailed description of generics in Java ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter A Simple Example: Magical Locker class Locker { private T content; public Locker( ) { store(null); } public T retrieve( ) { return content; } public void store(T item) { content = item; } Generic ClassUsage //Declaration Locker lockerOne; Locker lockerTwo; //Creation lockerOne = new Locker (); lockerTwo = new Locker (); //Usage lockerOne.store(“Hello”); lockerTwo.store(new Integer(10));...

Generics Illustrated ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Multiple Type Parameters A generic class can be specified with multiple type parameters T1 and T2 can be of any type ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter class SecondLocker { private T1 content1; private T2 content2;... public T1 retrieveFirstItem( ) { return content1; }... public T2 retrieveSecondItem( ) { return content2; }... } SecondLocker locker2;

Generics Support Type Safety Without generic classes, we must always face the danger of runtime error. For example: ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter ArrayList list = new ArrayList(); list.add(“Hello”); Integer intObj = (Integer) list.get(0); //Runtime type cast error By specifying the pluggable type, we can detect such error at the compile time ArrayList list;... list = new ArrayList ; //Compilation error

Bounded Types With the Locker generic class, we can plug in any class its type parameter. We can restrict the classes that can be specified the type parameter. Restricted type parameters are called bounded types. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter class FourthLocker {... } FourthLocker one; FourthLocker two; FourthLocker three; FourthLocker nogood; Valid NOT Valid

Wildcard Types The question mark symbol ? designates the wildcard type that allows any type ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter class FifthLocker {... public boolean isSameValue(FifthLocker item) { return this.retrieve().doubleValue() == item.retrieve().doubleValue(); } This method declaration will allow us to pass an item of any valid FifthLocker concrete type.

Restrictions You cannot create an instance of the type parameter inside a generic class Static data members cannot be declared as the type parameter T The return type of static methods cannot be declared as the type parameter T ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Generics and Collections As seen in Chapter 10, generics support the creation of homogeneous collections Without generics, when we call methods of an element in a collection, we must typecast the element (even if all elements in the collection is of the same type) We use the same technique discussed so far to define our own generic collection classes –See Node and SimpleLinkedList ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

A Subclass of a Generic Superclass When defining a subclass of a generic superclass, we must include the type parameter of the superclass in the subclass public class LockerSub extends Locker { … } The subclass can include additional type parameter public class LockerSubTwo extends Locker { … } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Generic Interfaces In addition to generic classes, we can define generic interfaces interface MagicContainer { … } Similar to creating a subclass of a generic superclass, a class that implements a generic interface must include the type parameter of the interface class MagicTrunk implements MagicContainter {…} –Type paramenter in the class can be bounded class MagicTrunk implements MagicContainer { … } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter

Raw Types The following statements do not cause an error ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Locker locker = new Locker(); //Locker is a generic class locker.store(new Integer(4)); Why don’t we have to declare locker to be of type Locker ? If we don’t specify the concrete type for the type parameter when creating an instance of a generic class, a raw type is created The raw type is dangerous and sabotages the type safety benefit of generics The raw type is allowed simply to maintain the backward compatibility. Avoid using raw types.

Generics and Arrays An array of type-specified generic class is not allowed Locker [ ] lockers; lockers = new Locker [25]; //INVALID An array of raw type generic class or a wildcard generic class is allowed Locker[ ] rawLockers = new Locker[25]; //OKAY Locker [ ] wildLockers = new Locker [25]; //OKAY The best approach is not to use arrays with generics. Using an ArrayList or a LinkedList instead. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter