Presentation is loading. Please wait.

Presentation is loading. Please wait.

עקרונות תכנות מונחה עצמים תרגול 21: Generics

Similar presentations


Presentation on theme: "עקרונות תכנות מונחה עצמים תרגול 21: Generics"— Presentation transcript:

1 עקרונות תכנות מונחה עצמים תרגול 21: Generics

2 Outline Generic classes Generics & Inheritance Wild Cards characters
Case study: Generic Graph

3 Generic Classes – Motivation
List v = new ArrayList(); v.add("test"); Integer i = (Integer)v.get(0); => // runtime error List<String> v = new ArrayList<String>(); v.add("test"); Integer i = (Integer)v.get(0); => // compilation error

4 class name<T1, T2, ..., Tn> { /* ... */ }
Generic Classes A class is generic if it declares one or more type variables. A generic class is defined with the following format: class name<T1, T2, ..., Tn> { /* ... */ } The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1,T2, ..., and Tn. The type variables can be used as: Method parameters. Return value. Local variables.

5 Generic Classes – Example
public interface List<E> { void add(E x); Iterator<E> iterator(); } public interface Iterator<E> { E next(); boolean hasNext(); public class ArrayList<E> implements List<E> { private E[] _data; ...

6 Generic Classes vs Concrete Classes
Generic Class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All compiles to the same .class file. All uses the same .class file at runtime.

7 Generic Classes VS Concrete Classes (Example)
public class ArrayList<E> implements List<E> { private E[] _data; ... } List<String> v; v = new ArrayList<String>(); Generic class Compile to ArrayList.class Concrete class Uses ArrayList.class

8 Generics & Inheritance

9 Assignments roles: If a type A is of a sub-class of type B we will mark it as: 𝐴≼𝐵 We can assign (runtime) type A to a (compile time) variable B iff 𝐴≼𝐵

10 Using wild cards – Motivation
𝐷𝑜𝑔≼𝐴𝑛𝑖𝑚𝑎𝑙 Animal animal; animal = new Dog(); Cat cat = animal; // // compilation error!!! 𝐴𝑛𝑖𝑚𝑎𝑙≼𝐶𝑎𝑡 Vector<Animal> animals; Vector<Cat> cats = new Vector<Cat>(); animals = cats; animals.add(new Dog()); Cat cat = cats.elementAt(0); // // compilation error!!! 𝑉𝑒𝑐𝑡𝑜𝑟<𝐶𝑎𝑡>≼𝑉𝑒𝑐𝑡𝑜𝑟<𝐴𝑛𝑖𝑚𝑎𝑙> 𝐷𝑜𝑔≼𝐴𝑛𝑖𝑚𝑎𝑙

11 Using wild cards 𝐴≼𝐵 ↛𝐺<𝐴>≼𝐺<𝐵> 𝐴≼𝐵 𝑖𝑚𝑝𝑙𝑖𝑒𝑠:
We can read type B from A. We can write type A to B. We will use wild card (?) to represent an unknown type.

12 Using wild cards ? extends A – a variable type which can only be read from. We can write only null to it. ? super B – a variable type which can only be written to. We can read only Object from it. 𝐴≼𝐵→𝐺<𝐴> ≼𝐺<? 𝑒𝑥𝑡𝑒𝑛𝑑𝑠 𝐵>. 𝐴≼𝐵→𝐺<𝐵> ≼𝐺<?𝑠𝑢𝑝𝑒𝑟 𝐴>.

13 Using wild cards – example:
public static void makeNoise (Vector<Animal> animals) { for (Animal animal:animals) { animal.say(); } { Vector<Cat> cats = new Vector<Cat>(); makeNoise(cats); => Correction: public static void makeNoise (Vector<? extends Animal> animals) // compilation error!!!

14 Using wild cards – example:
Vector<? extends Animal> animals = null; Vector<Cat> cats = new Vector<Cat>(); animals = cats; animals.add(new Dog()); Cat cat = cats.elementAt(0); => // compilation error!!! Correction: animals.add(null); - The only one that allows.

15 Using wild cards – examples:
Vector<? extends Animal> animals = new Vector<Cat>(); Vector<? extends Cat> cats = new Vector<Animal>(); Vector<? super Animal> animals2 = new Vector<Cat>(); Vector<? super Cat> cats2 = new Vector<Animal>(); animals.add(new Cat()); cats.add(new Cat()); animals.add(null); Cat cat = animals.elementAt(0); Animal animal = cats.elementAt(0); Cat cat2 = animals2.elementAt(0); Animal animal2 = animals2.elementAt(0); Object o = animals2.elementAt(0); // compilation error // compilation error // compilation error // compilation error // compilation error // compilation error // compilation error

16 Question from 2013 test. נתון שהמחלקה Bיורשת מהמחלקה A, והמחלקה G היא גנרית עם פרמטר יחיד. מה צריך להיות הפרמטר X כדי שההשמה הבאה תהיה תקפה ע"פ כללי הטיפוסים של ג'אווה? G<X> g = new G<B>(); א. A ב. extends A ? ג. super A ? ד. super B ? ה. תשובות ב, ד נכונות ו. תשובות א ,ג נכונות

17 Question from 2013 test. נתון שהמחלקה Bיורשת מהמחלקה A, והמחלקה G היא גנרית עם פרמטר יחיד. מה צריך להיות הפרמטר X כדי שההשמה הבאה תהיה תקפה ע"פ כללי הטיפוסים של ג'אווה? G<X> g = new G<B>(); א. A ב. extends A ? ג. super A ? ד. super B ? ה. תשובות ב, ד נכונות ו. תשובות א ,ג נכונות

18 Case study: Generic Graph

19 Graph

20 Graphs can model The internet Maps Social networks

21 Graph Representation

22 The vertex class

23 The Graph Class

24 The Graph Class

25 Vertex class - code

26 Graph class - code

27 DFS Tour

28 DFS Tour

29 Execution example

30 The Weighted interface

31 Weight query

32 Usage example

33 Usage example (cont.)

34 Exception handling

35 Exception handling

36 Summary Generic class ( List<E> ) 𝐴≼𝐵 ↛𝐺<𝐴>≼𝐺<𝐵>
? extends A ? super A

37 Summary Generic class ( List<E> ) 𝐴≼𝐵 ↛𝐺<𝐴>≼𝐺<𝐵>
? extends A ? super A


Download ppt "עקרונות תכנות מונחה עצמים תרגול 21: Generics"

Similar presentations


Ads by Google