Outline Generic classes Generics Inheritance Wild Cards characters

  • Slides: 37
Download presentation

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

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

Generic Classes – Motivation List v = new Array. List(); v. add("test"); Integer i

Generic Classes – Motivation List v = new Array. List(); v. add("test"); Integer i = (Integer)v. get(0); => // runtime error List<String> v = new Array. List<String>(); 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.

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<T 1, T 2, . . . , Tn> { /*. . . */ } The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T 1, T 2, . . . , and Tn. The type variables can be used as: Method parameters. Return value. Local variables.

Generic Classes – Example public interface List<E> { void add(E x); Iterator<E> iterator(); }

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

Generic Classes vs Concrete Classes Generic Class declaration defines a set of parameterized types,

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 Array. List<E> implements List<E> { private

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

Generics & Inheritance

Generics & Inheritance

Assignments roles:

Assignments roles:

Using wild cards – Motivation Animal animal; animal = new Dog(); Cat cat =

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; // compilation error!!! animals. add(new Dog()); Cat cat = cats. element. At(0);

Using wild cards

Using wild cards

Using wild cards

Using wild cards

Using wild cards – example: public static void make. Noise (Vector<Animal> animals) { for

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

Using wild cards – example: Vector<? extends Animal> animals = null; Vector<Cat> cats =

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

Using wild cards – examples: Vector<? extends Animal> animals = new Vector<Cat>(); Vector<? extends

Using wild cards – examples: Vector<? extends Animal> animals = new Vector<Cat>(); Vector<? extends Cat> cats = new Vector<Animal>(); // compilation error Vector<? super Animal> animals 2 = new Vector<Cat>(); // compilation error Vector<? super Cat> cats 2 = new Vector<Animal>(); animals. add(new Cat()); // compilation error cats. add(new Cat()); // compilation error animals. add(null); Cat cat = animals. element. At(0); // compilation error Animal animal = cats. element. At(0); Cat cat 2 = animals 2. element. At(0); // compilation error Animal animal 2 = animals 2. element. At(0); Object o = animals 2. element. At(0); // compilation error

Case study: Generic Graph

Case study: Generic Graph

Graph

Graph

Graphs can model The internet Maps Social networks …

Graphs can model The internet Maps Social networks …

Graph Representation

Graph Representation

The vertex class

The vertex class

The Graph Class

The Graph Class

The Graph Class

The Graph Class

Vertex class - code

Vertex class - code

Graph class - code

Graph class - code

DFS Tour

DFS Tour

DFS Tour

DFS Tour

Execution example

Execution example

The Weighted interface

The Weighted interface

Weight query

Weight query

Usage example

Usage example

Usage example (cont. )

Usage example (cont. )

Exception handling

Exception handling

Exception handling

Exception handling

Summary

Summary

Summary

Summary