Java Generics and Subtyping Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java two neighbors 1. Initialization: 2.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Data Structures & Java Generics Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Java Generics.
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
Hashing Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Abstractions, Collections & Data Structures Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 27: More on ArrayList, Reference Semantics Command Line Arguments reading:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Effective Java: Generics Last Updated: Spring 2009.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Polymorphism (generics) CSE 331 University of Washington.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Question of the Day  Move one matchstick to produce a square.
Interfaces & Sub-types Weiss sec Scenario Instructor says: “Implement a class IntegerMath with two methods pow and fact with the following signatures:
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
using System; namespace Demo01 { class Program
Introducing Java Generics and Collections
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Java Lesson 36 Mr. Kalmes.
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
CSE 143 Lecture 5 References and Linked Nodes
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
Presentation transcript:

Java Generics and Subtyping Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

Which of these are legal? String s = new Object(); Object o = new String(); Set s1 = new HashSet(); Set s2 = new HashSet ();

Subtypes If B is a subtype of A, then a B can be used where an A is expected For example, if a method is declared to return an Object, you can return a String

Subtypes and Collections A HashSet is a subtype of Set String is a subtype of Object But a HashSet is not a subtype of HashSet and ArrayList > is not a subtype of List >

Why not?? void f(HashSet set) { ??? } void g(HashSet set) { f(set); for(String s : set) System.out.println(s.substring(0,1)); }

Why not?? void f(HashSet set) { set.add(new Integer(42)); } void g(HashSet set) { f(set); for(String s : set) System.out.println(s.substring(0,1)); }

Wildcards in generics This is somewhat advanced material, but useful You can define a function as: void f(Set set) {... } What does that mean? It means that f takes as a parameter a Set of some type that is unnamed and unknown So the Set passed to f might be a Set, or a Set, or a Set >, or...

yes, but what does that mean If f is defined to take a Set set Then set is a read-only set You can take a value out; the only thing you know about it is that what you take out is some subtype of Object But you can’t add a value to set You don’t know what the type of things you are allowed to add to set is

This can get more complicated If you define void f(Set > set) Can pass to f: HashSet >

Arrays and Subtyping public class Test2 { public static void f(Object a[]) { System.out.println(a[0]); } public static void g(Object a[]) { a[0] = new Integer(17); } public static void main(String[] args) { String[] s = {"a", "b"}; f(s); // OK g(s); }

Markov Chain Sunny DayCloudy Day 90% 50% 10%

Second Order Markov Chain Random Text Generation Project

Links FSM Generics - erics-tutorial.pdf erics-tutorial.pdf