Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki

Slides:



Advertisements
Similar presentations
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Advertisements

Generic programming in Java
ITEC200 – Week03 Inheritance and Class Hierarchies.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
Java 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.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
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.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
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.
 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.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Computer Engineering Rabie A. Ramadan Lecture 5.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Types in programming languages1 What are types, and why do we need them?
CIS 270—Application Development II Chapter 18-Generics.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
(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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 20 Generic Classes and Methods
Chapter 19 Generics Dr. Clincy - Lecture.
Generics.
Generic programming in Java
Chapter 19 Generics.
Java Programming Language
Defining Classes and Methods
Defining Classes and Methods
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki

2 When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection  inconvenient and unsafe. With generics, the compiler will know the object type (same for all objects stored), so that it can be checked. Generics The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

3 When we declare c to be of type Collection, this tells us something about the variable c that holds true wherever and whenever it is used, Why use generics and the compiler guarantees it (assuming the program compiles without warnings). A cast, on the other hand, tells us something the programmer thinks is true at a single point in the code, and the VM checks whether the programmer is right only at run time.

4 Small excerpt from the definitions of the interfaces List and Iterator in package java.util: Might seem very similar to C++ templates so far... Understanding generics public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); }

5...but it is different than C++ templates In C++, templates are kind of intelligent macros. Not so in Java. There aren’t multiple copies of the code: not in source, not in binary, not on disk and not in memory. Understanding generics Having vector and vector in our program, we also have two copies of the code: a single copy for each used type (=template parameter). In other words, it is a compile-time mechanism. An analogy to plain methods: parameters in methods are values of a given type; parameters of generics are types.

6...the following piece of code: List l1 = new ArrayList (); List l2 = new ArrayList (); System.out.println(l1.getClass() == l2.getClass()); Consequently... A method has formal value parameters that describe the kinds of values it operates on; a generic declaration has formal type parameters. Understanding generics When a method is invoked, actual arguments are substituted for the formal parameters, and the method body is evaluated. When a generic declaration is invoked, the actual type arguments are substituted for the formal type parameters....prints true.

7 non-generic collections – lots of casts required. No compile-time checks. generic collections – homogeneous, no casts required, compile-time checking. Generics in depth

8 Type variable: “placeholder” for an unknown type. NOT REALLY A TYPE: not allowed in new expressions; cannot be derived from. Definition of generic types

9 Bounds = supertype of a type variable (Comparable is the supertype in the example above). Type parameter bounds What for? To make available non-static members of a type variable. Limitations: gives no access to constructors or static methods.

10 with concrete type arguments, without type arguments (yes!), with wildcard arguments. Concrete instantiation Raw type (permitted for compatibility – mixing generics with old code) Using generic types

11 Wildcard instantiation

12 Wildcards

13 Consider a method that draws objects from a class hierarchy of shapes. Cannot draw e.g. a list of circles because List is NOT a subtype of List. Bounded wildcard example

14 Fix with ‘?’ ?? Use upper bound wildcard to solve the problem. Bounded wildcard example

15 Defining a generic type – case study

16 Defining constructors. Naïve approach…

17 Defining constructors. Quick and dirty fix...

18 Same type constructor argument

19 useful cases are also rejected Note: the abstract class java.lang.Number is the superclass of classes Byte, Double, Float, Integer, Long, and Short. Problem with the previous example

20 Compatible type constructor argument

21 Equivalent implementation