1 / 14

Practical Session 12: Generics

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

venice
Download Presentation

Practical Session 12: Generics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical Session 12:Generics

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

  3. 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.

  4. Generic Classes – Example public interface List<E> { void add(E x); Iterator<E> iterator(); } publicinterface Iterator<E> { E next(); booleanhasNext(); } public class ArrayList<E> implementsList<E> { private E[] _data; ... }

  5. Generic Classes vsConcrete 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.

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

  7. 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

  8. Using wild cards – Motivation Animal animal; animal = new Dog(); Cat cat = animal; // compilation error!!! // compilation error!!! Vector<Animal> animals; Vector<Cat> cats = new Vector<Cat>(); animals = cats; animals.add(new Dog()); Cat cat = cats.elementAt(0);

  9. 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.

  10. Using wild cards ? extends A – a variable type witch can only be read from. It can be assign to any B type where . We can write only null to it. ? super B – a variable type witch can only be written to. We can assign to it any A type where . We can read only object from it.

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

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

  13. Using wild cards – examples: // compilation error // compilation error // compilation error // compilation error // compilation error // compilation error // compilation error // compilation error 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); animals2.add(new Cat()); cats2.add(new Animal()); Cat cat2 = animals2.elementAt(0); Animal animal2 = animals2.elementAt(0); Object o = animals2.elementAt(0);

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

More Related