generic classes
play

Generic classes Declaration Use Annotations 54 Generic classes - PowerPoint PPT Presentation

Generic classes Declaration Use Annotations 54 Generic classes Declaration add generic types between angle brackets: public class MyStack < E,F >{ E item; } 55 Generic Classes Use MyStack<Integer> ms= new


  1. Generic classes • Declaration • Use • Annotations 54

  2. Generic classes Declaration • add generic types between angle brackets: public class MyStack < E,F >{ E item; } 55

  3. Generic Classes Use MyStack<Integer> ms= new MyStack<Integer>(); 56

  4. Generic Classes Annotations • public int count(MyClass<?> mc){...} • public int count(MyClass<? extends String> mc){...} • public int count(MyClass<? extends String & Countable> mc){...} 57

  5. Why not subtype generics MyStack<Integer> myStack=new MyStack<Integer>(); public static void add(Stack<Object> st, Object o){ st.add(o); } 58

  6. Example: MySmallStack public void add(E value){ Element<E> e; e= new Element<E>(value); e.next=this.first; this.first=e; } ... MySmallStack<Integer> myStack = new MySmallStack<Integer>(); myStack.add(1); 59

  7. Nested Classes • Classes that are declared in a file, along another one • If outside a class: static • If inside, it depends... they can be inner classes 60

  8. Inner Classes • A particular type of nested classes • Anonymous • Method-scope • Class scope 61

  9. Anonymous Inner Classes • Declared at instanciation public void wave(){ Object o = new Object(){ public Object clone(){ return this; } }; ... } 62

  10. Method Inner classes • declared in a method • visible only in the method // method declaration public void wave(){ class Hello2 extends HelloWorld{ }; ... } 63

  11. Class Inner Classes • Declared in the class • visibility decides on access control outside of the class public class HelloWorld extends Object{ class Hello2 extends HelloWorld{ }; // method declaration public void wave(){ ... } } ... HelloWorld.Hello2 h=new HelloWorld.Hello2(); 64

  12. Some interesting classes • Object • String • System • Vector 65

  13. Object • See API 66

  14. String • See API 67

  15. System • See APIs 68

  16. Vector • See APIs 69

  17. Java Basics Part 3 - Exceptions Manuel Oriol, April 13th, 2006

  18. Throwable • The Throwable interface is meant to represent computational events that can interrupt the current computation • Computation can occur after the event is handled 71

  19. Exception • Exceptions represent events that are meant to be treated. • Whenever a method may trigger an exception, it is required that it declares so (modulo conformance). Except for RuntimeExceptions. 72

  20. Runtime Exceptions AnnotationTypeMismatchException, ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, EnumConstantNotPresentException, EventException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IncompleteAnnotationException, IndexOutOfBoundsException, JMRuntimeException, LSException, MalformedParameterizedTypeException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, RejectedExecutionException, SecurityException, SystemException, TypeNotPresentException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException ... 73

  21. Error • Meant to represent an unrecoverable error • Can be recovered still... • Example: AnnotationFormatError, AssertionError, AWTError, CoderMalfunctionError, FactoryConfigurationError, LinkageError, ThreadDeath, TransformerFactoryConfigurationError, VirtualMachineError 74

  22. Throw, throws • it is possible to throw an exception manually by using: throw an_exception ; • methods that may fail due to an exception (non-runtime) have to indicate it: public void m() throws MyException{...} 75

  23. try...catch... finally try{ ... } catch ( MyException1 e ){...} catch ( MyException2 e ){...} ... finally{...} 76

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend