Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Wildcards in the Java™ Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Generic programming in Java
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
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 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
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-1 Generic Types in Java Format for a generic (parameterized) type and instantiation of a generic type.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
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.
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.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Information and Computer Sciences University of Hawaii, Manoa
Generics CompSci 230 S Software Construction.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Types in programming languages1 What are types, and why do we need them?
Introduction to Generics
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
CMSC 330: Organization of Programming Languages Java Generics.
Java 1.5 Generics Amr Ali Software Engineer, IBM-Egypt EG-JUG.
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 & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
Polymorphism (generics) CSE 331 University of Washington.
Java Type System and Object Model Horstmann ch , 7.7.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION GENERICS/PARAMETRIC POLYMORPHISM Autumn 2011 We We Abstraction.
©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.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
List Interface and Linked List Mrs. Furman March 25, 2010.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Iteration Abstraction SWE Software Construction Fall 2009.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
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.
James Wilcox / Winter 2016 CSE 331 Software Design and Implementation Lecture 14 Generics 2.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
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.
Java Generics.
Implementing ArrayList Part 1
תוכנה 1 בשפת Java שיעור מספר 9: "ירושה נכונה" (הורשה II)
Generics (Parametric Polymorphism)
Generics 27-Nov-18.
ArrayLists 22-Feb-19.
Generics 2-May-19.
COMP204 Bernhard Pfahringer (with input from Robi Malik)
Presentation transcript:

Lists Discussion F

Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards

Iterator List lint = new ArrayList (); //1 lint.add(new Integer(100)); //2 Iterator intIter = lint.iterator(); //3 Integer i1 = intIter.next(); //4 lint.add(101); //5 int i2 = intIter.next(); //6 ConcurrentModificationException

Erasure public class X { public X();} //7 public class Y extends X { public Y();} //8 List lx = new ArrayList (); //9 List ly = new ArrayList (); //10 System.out.println(lx.getClass()==ly.getClass());//12 true

Erasure (2) public class X { public X();} //7 public class Y extends X { public Y();} //8 List lx = new ArrayList (); //9 List ly = new ArrayList (); //10 if (lx instanceof ArrayList ) {}//unchecked warning List otherlx = (List ) lx;

Subtype public class X { public X();} //7 public class Y extends X { public Y();} //8 List lx = new ArrayList (); //9 List ly = new ArrayList (); //10 lx = ly; //11 Compiler Error

Subtype (2) void printCollection(Collection c) { //13 Iterator i = c.iterator(); while(i.hasNext()) { System.out.println(i.next()); } void printCollection(Collection c) { //14 for (Object o: c) { System.out.println(o); } printCollection(lint); //??

Bounded Wildcards ● Used to express polymorphism ● Wildcard, means some type – ● lower bound, some super type of T – ● upper bound, some sub class of B –

Wildcards void printCollection(Collection c){ for (Object o: c) { System.out.println(o); } Collection c = new ArrayList (); c.add(new Object()); // Error void printCollection(Collection c){ for (Shape s: c) { System.out.println(s.area()); }

Generic Functions void arrayToCollection(T[] a, Collection c) { for (T t: a) { c.add(t); } Integer [] a = new Integer[10];... Collection c = new ArrayList (); arrayToCollection(a, c); No need to pass actual type, compiler infers it They can have bounds too Used when arguments/return type are correlated

Inner Classes ● Block Scoped – definition similar to definition of a field or a method ● Static – do not have the block scope

Snippets // 1 public class X { //1 private int fx; public class I { int fi; public I() { fi = fx; } } X.I ci = new X.I(); java X$I // to run main method in X.I

// 2 public class X { //2 private int fx; private I i = new I(); private int fx2 = i.fi; public class I { private int fi; public I() { fi = fx; } }

// 3 public class X { //3 int fx; public static class I { int fi; public I() { fi = fx; } }

DisInherit Methods ● Not really, has to satisfy Is-a ● Illegal to narrow visibility ● Can reimplement and throw exception

DisInherit public class X { public void function() {} } public class Y extends X { private void function() {} // compiler error } public class Z extends X { private void function() { throw new NoSuchMethodException(); }

Lisp List carcdrcarcdr

Lisp List Interface public interface LispList { public int length(); public E first (); public LispList rest (); }

Lisp List public Cons implements LispList { E car; LispList cdr; public Cons(E e) {car = e;} public Cons(LispList onelist, LispList otherlist) {} public int length() {} public E first () {return car;} public LispList rest () {return cdr;} }

Lisp List ● Empty list ● How to construct from two lists? ● How to find length?

List of Arrays

List Of Arrays ● Each node has same capacity – can compute how many nodes from start the required index will be ● nodes have different capacity – can balance the list length

Using List public class ListOfArrays { int totalCount; int totalCapacity; int nodeCapacity; List list; private void checkIndex(int idx) {} public E get(int idx){} public void set(int idx, E e) {} }

List of Arrays public class ListOfArrays { int totalCount; int totalCapacity; List > list; private class DataNode { int count; int capacity; E [] array; } private void checkIndex(int idx) {} public E get(int idx){} public void set(int idx, E e) {} }