1 / 20

Generics

Generics. OOP Tirgul 8 2006. What is it good for ?. Without Using generics, the code looks like this:. Stack myStack = new Stack() ; // old version (1.4.2) myStack.push(new Integer(0)) ; int x = ((Integer) myStack.pop()).intValue() ; // annoying cast.

rumor
Download Presentation

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. Generics OOP Tirgul 8 2006

  2. What is it good for ? Without Using generics, the code looks like this: Stack myStack = new Stack() ; // old version (1.4.2) myStack.push(new Integer(0)) ; int x = ((Integer) myStack.pop()).intValue() ; // annoying cast Using generics, the code looks like this: Stack<Integer> myStack = new Stack<Integer>() ; myStack.push(new Integer(0)) ; int x = myStack.pop().intValue() ; // no cast is needed More readable and more typesafe!

  3. Implementation IssuesDefining a Generic Stack public interface Stack<E> { public boolean empty() ; public E peek() ; public E pop() ; public E push (E item) ; public E peek() ; } Using a single letter in UPPER CASE for the type is the acceptable naming convention

  4. Implementation IssuesGenerics and inheritance Stack<Object> objectStack = new Stack<Object>() ; Stack<Integer> integerStack=objectStack ; // illegal assignment Although Integer Inherits Object, Stack<Integer> is a different type from Stack<Object>

  5. Remark Stack<String> objectStack = new Stack<String>() ; Stack myStack = objectStack ; // is this legal ? (compile // time warning) Integer x = new Integer(0); myStack.push(x) ; // what happens here? (Runtime error)

  6. Implementation IssuesGenerics and inheritance(2) public void printAndEmptyStack(Stack stack) { while (!stack.empty()) System.out.println(stack.pop()); } What is the problem when using generics ? How do we implement it using generics ?

  7. Implementation IssuesGenerics and inheritance(3) Using wildcards : public void printAndEmptyStack( Stack<?> stack) { while (!stack.empty()) System.out.println(stack.pop()); } Stack<?> is a called “stack of unknowns”.

  8. Implementation IssuesGenerics and inheritance(4)

  9. Implementation IssuesGenerics and inheritance(5) Assume we want to print and empty only stacks of animals, i.e Stack<Animal>, Stack<Zebra> etc. The correct syntax is : public void printAndEmptyStack ( Stack<? extends Animal> stack) { while (!stack.empty()) System.out.println(stack.pop()); } Note: Stack<Animal> given to this function is a legal type.

  10. Implementation IssuesGenerics and inheritance(6) Is this code legal ? public void addElement ( Stack<? extends Animal> s) { s.add (new Frog() ) ; } What if the parameter given to the method was Stack<Zebra> ?

  11. Generic Methods Version 1: public static void insertElementToStack (Object element, Stack<?> c) { if (element != null) c.push(element) ; //compile time error } Version 2 : public static void insertElementToStack (? element, Stack<?> c) { if (element != null) c.push(element) ; } // compile time error – no such type “?”

  12. Generic Methods(2) Version 3: public static <T> void insertElementToStack (T element, Stack<T> c) { if (element != null) c.push(element) ; } // this version is the way to do it

  13. Generic Methods(3) public static <T,E extends T> void insertElementToStack (E element, Stack<T> c) { if (element != null) c.push(element) ; } // this allows inserting elements which inherit from a base // class

  14. Arrays public class Stack<E> { private final static int INITIAL_CAPACITY = 100 ; private E[] _stack = new E[INITIAL_CAPACITY]; // compile time // error }

  15. Exercises Is the following legal : public static void add (String s , Stack<String> stack) { stack.push(s) ; } public static void main(string args[] ) { Stack<String> s = new Stack<String>(); add( “hapoel”, s) ; }

  16. Exercises(2) Is the following legal : public static void add (String s , Stack<?> stack) { stack.push(s) ; } public static void main(string args[] ) { Stack<String> s = new Stack<String>() ; add( “hapoel”, s) ; }

  17. Exercises(3) Is the following legal : public static void addNull( Stack<?> stack) { stack.push( null ) ; } public static void main(string args[] ) { Stack<String> s = new Stack<String>(); addNull(s) ; }

More Related