Download presentation
Presentation is loading. Please wait.
Published byAnabel Morrison Modified over 9 years ago
1
עקרונות תכנות מונחה עצמים תרגול 10: Generics
2
Outline Generic classes Generics & Inheritance Wild Cards Case study: Generic Graph
3
Generic Classes – Motivation List v = new ArrayList(); v.add("test"); Integer i = (Integer)v.get(0); // runtime error List v = new ArrayList (); v.add("test"); Integer i = (Integer)v.get(0); // compilation error
4
Generic Classes A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. The type variables can be used as: Method parameters. Return value. Local variables.
5
Generic Classes – Example public interface List { void add(E x); Iterator iterator(); } public interface Iterator { E next(); boolean hasNext(); } public class ArrayList implements List { 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 implements List { private E[] _data;... } List v; v = new ArrayList (); Generic class Compile to ArrayList.class Concrete class Uses ArrayList.class
8
Generics & Inheritance
9
Assignments roles:
10
Using wild cards – Motivation Vector animals; Vector cats = new Vector (); animals = cats; animals.add(new Dog()); Cat cat = cats.elementAt(0); // compilation error!!! Animal animal; animal = new Dog(); Cat cat = animal; // compilation error!!!
11
Using wild cards
13
Using wild cards – example: public static void makeNoise (Vector animals) { for (Animal animal:animals) { animal.say(); } { Vector cats = new Vector (); makeNoise(cats); public static void makeNoise (Vector animals) // compilation error!!!
14
Using wild cards – example: Vector animals = null; Vector cats = new Vector (); animals = cats; animals.add(new Dog()); Cat cat = cats.elementAt(0); // compilation error!!! animals.add(null); - The only one that allows.
15
Using wild cards – examples: Vector animals = new Vector (); Vector cats = new Vector (); Vector animals2 = new Vector (); Vector cats2 = new Vector (); 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
16
Question from 2013 test. נתון שהמחלקה B יורשת מהמחלקה A, והמחלקה G היא גנרית עם פרמטר יחיד. מה צריך להיות הפרמטר X כדי שההשמה הבאה תהיה תקפה ע " פ כללי הטיפוסים של ג ' אווה ? G g = new G (); א. A ב. extends A ? ג. super A ? ד. super B ? ה. תשובות ב, ד נכונות ו. תשובות א, ג נכונות
17
Case study: Generic Graph
18
Graph
19
Graphs can model The internet Maps Social networks …
20
Graph Representation
21
The vertex class
22
The Graph Class
23
Vertex class - code
24
Graph class - code
25
DFS Tour
27
Execution example
28
The Weighted interface
29
Weight query
30
Usage example
31
Usage example (cont.)
32
Exception handling
34
Summary
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.