Generics and Type Safety Recitation – 4/24/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Java Review Interface, Casting, Generics, Iterator.
Written by: Dr. JJ Shepherd
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
Generic programming in Java
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Dynamic Data Structures and Generics Recitation – 11/(14,15)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
30-Jun-15 Generics. Arrays and collections In Java, array elements must all be of the same type: int[] counts = new int[10]; String[] names = { "Tom",
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
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.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Chapter 3 Introduction to Collections – Stacks Modified
Intro to OOP with Java, C. Thomas Wu
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.
1 Programming Section 11 James King 12 August 2003.
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.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
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.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
 2005 Pearson Education, Inc. All rights reserved Generics.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
Types in programming languages1 What are types, and why do we need them?
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
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
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.
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.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
Lecture 9: Lists MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture we will learn…. ArrayList – These are re-sizeable.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 20 Generic Classes and Methods
Object Oriented Programming (OOP) LAB # 8
Interfaces.
Java Programming Language
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Generics and Type Safety Recitation – 4/24/2009 CS 180 Department of Computer Science, Purdue University

Announcement Project 8 due Wed, April 29 at 10 pm. All questions on the class newsgroup.

Generics Motivation In Java, array elements must all be of the same type:  int[] counts = new int[10]; Hence, arrays are type safe: The compiler will not let you put the wrong kind of thing into an array A collection, such as a Vector or the non- parameterized ArrayList, cannot hold primitives, but will accept any type of Object:  ArrayList someStuff = new ArrayList(); someStuff.add("A String is an Object"); someStuff.add(10); Is not type safe; Making a collection type safe is a tedious process

Generics J2SE 5.0 provides compile-time type safety with the Java Collections framework through generics Generics allows you to specify, at compile-time, the types of objects you want to store in a Collection. Then when you add and get items from the list, the list already knows what types of objects are supposed to be acted on So you don't need to cast anything. The "<>" characters are used to designate what type is to be stored. If the wrong type of data is provided, a compile-time exception is thrown.

Problem: Develop a Roster Purdue need rosters for undergraduate students, graduate students and employees. Each department needs rosters for their students and employees. Suppose there are ten departments. How many roster classes do we need to write?

Raw Solution The simplest solution is:  Write a class Roster which accept Objects.  We can have three objects: undergraduateRoster, graduateRoster and employeeRoster.  We can manually make sure correct objects are put in correct rosters.  When we get data from a roster, type casting is needed. If someone add an Integer to graduateRoster by mistake, what happens?

Inheritance Solution An intuitive solution after learning inheritance is like this:  Write a super class Roster.  Write three subclasses UndergraduateRoster, GraduateRoster and EmployeeRoster.  In EmployeeRoster, Student objects are not accepted. It looks reasonable, but what happens if a new roster like TARoster is needed? What happens if ten more different rosters are needed?

Generics Solution Now we use generics to solve it.  Write a class Roster.  If we want a roster of undergraduate students, we can use Roster. What’s the benefit here?  Easy to add new rosters.  Only need one class.  Do not accept incompatible objects.  Still has flexibility: Roster can accept UndergraduateStudent and GraduateStudent objects.

Some Issues about It We can define a Roster to make sure all rosters can do something with class Person. What’s the relationship between Roster and Roster ?  Roster studentRoster = new Roster ();correct?

Problem not Solved Yet! Each department needs rosters for their students and employees. Do we need Roster, T1 for student or employee and T2 for department? What do you think? Remember Roster has no relationship with Roster.

Generics, Inheritance and Interface A subclass of a generic class must be a generic class. The super class of a generic class may not be a generic class.  Object class is a good example. A generic interface can only be implemented by corresponding generic class. A generic class can implement non-generic interface.

Raw Types A generic class can be used to produce different classes like Roster described previously. However, Java still allow the declaration of Roster objects.  Roster rawRoster = new Roster(); This roster may contain any kind of objects like Integer, Circle, Car… If we have Roster, can it still accept any kinds of objects?  No!

Quiz If you are required to deal with complex numbers (a + bi). If a and b should have the same type but they can be Integer, Long, Float or Double. How will you define your classes? If a and b may not have the same type, what would you do? No need to write a complete class definition.