COMP204 Bernhard Pfahringer (with input from Robi Malik)

Slides:



Advertisements
Similar presentations
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Advertisements

Java Generics.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
Generic types for ArrayList Old Java (1.4 and older): ArrayList strings = new ArrayList(); strings.enqueue(“hello”); String word = (String) strings.get(0);
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 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Generics OOP Tirgul What is it good for ? Stack myStack = new Stack() ; // old version (1.4.2) myStack.push(new Integer(0)) ; int x = ((Integer)
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Chapter 3 Introduction to Collections – Stacks Modified
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.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
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.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CMSC 330: Organization of Programming Languages Java Generics.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Java Generics And collections
Design issues for Object-Oriented Languages
Generics, Exceptions and Undo Command
Chapter 6 – Data Types CSCE 343.
ADT’s, Collections/Generics and Iterators
Java Generics.
Generic(Parameterized ) Types
Chapter 20 Generic Classes and Methods
Java Generics Lecture 14 CS2110 – Fall 2016
Topic 6 Generic Data Structures
Generics.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Advanced Programming Behnam Hatami Fall 2017.
Abstract Data Types (ADT)
Generics A Brief Review 16-Nov-18.
Chapter 19 Generics Dr. Clincy - Lecture.
עקרונות תכנות מונחה עצמים תרגול 21: Generics
Generics and Type Parameters
Generics (Parametric Polymorphism)
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Advanced Programming in Java
Generics 27-Nov-18.
Generics.
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
slides created by Alyssa Harding
Review: libraries and packages
Generics 2-May-19.
Chapter 19 Generics.
Presentation transcript:

COMP204 Bernhard Pfahringer (with input from Robi Malik) Java Generics COMP204 Bernhard Pfahringer (with input from Robi Malik)

Generic types for collections Old Java (1.4 and older): List strings = new ArrayList(); strings.add(“hello”); String word = (String) strings.get(0); New (since 1.5): List<String> strings = new ArrayList<String>(); String word = strings.get(0);

Advantages Better readability Better type-safety: no casts (runtime checks), compiler can already catch problems

Writing your own generic code public class Stack<E> { public void push(E element) { contents.add(element); } public E pop() { int top = contents.size()-1; E result = contents.get(top); contents.remove(top); return result; private List<E> contents = new ArrayList<E>();

Formal type parameter public class Stack<E> { … } convention: Short (single-char) uppercase can be used wherever a Type is needed will be replaced with actual Type

Problems with sub-types class Student extends Person { .. } List<Student> students = new ArrayList<Student>(); List<Person> people = students; // should this be possible? -> no

No, because Person sam = new Person(); people.add(sam); // if this was legal, we would have just sneaked a non-student onto the students list on the other hand, how do you write generic code then accepting lists of sub-types of Persons ???

Wildcards void printCollection(Collection<?> c) { for(Object o: c) { System.out.println(o); } // only read access (everything is an Object), but cannot add, because do not know correct type (except null, which is any type)

Bounded wildcards public void drawAll(Collection<? extends Shape> c) { for(Shape shape: c) { shape.draw(); } // again, only read access, allows collections of Shape or any sub type of Shape