עקרונות תכנות מונחה עצמים תרגול 10: Generics. Outline Generic classes Generics Inheritance Wild Cards Case study: Generic Graph

Embed Size (px)

DESCRIPTION

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

Citation preview

10: Generics Outline Generic classes Generics & Inheritance Wild Cards Case study: Generic Graph 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 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. 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;... } 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. 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 Generics & Inheritance Assignments roles: 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!!! Using wild cards 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!!! 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. 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 Question from 2013 test. B A, G . X " ' ? G g = new G (); . A . extends A ? . super A ? . super B ? . , . , Case study: Generic Graph Graph Graphs can model The internet Maps Social networks Graph Representation The vertex class The Graph Class Vertex class - code Graph class - code DFS Tour Execution example The Weighted interface Weight query Usage example Usage example (cont.) Exception handling Summary