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

generic classes
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Generic classes

  • Declaration
  • Use
  • Annotations

54

slide-2
SLIDE 2

Generic classes Declaration

  • add generic types between angle brackets:

public class MyStack<E,F>{ E item; }

55

slide-3
SLIDE 3

Generic Classes Use

MyStack<Integer> ms= new MyStack<Integer>();

56

slide-4
SLIDE 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

slide-5
SLIDE 5

Why not subtype generics

MyStack<Integer> myStack=new MyStack<Integer>(); public static void add(Stack<Object> st, Object o){ st.add(o); }

58

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 8

Inner Classes

  • A particular type of nested classes
  • Anonymous
  • Method-scope
  • Class scope

61

slide-9
SLIDE 9

Anonymous Inner Classes

  • Declared at instanciation

public void wave(){ Object o = new Object(){ public Object clone(){ return this; } }; ... }

62

slide-10
SLIDE 10

Method Inner classes

  • declared in a method
  • visible only in the method

// method declaration public void wave(){ class Hello2 extends HelloWorld{ }; ... }

63

slide-11
SLIDE 11

Class Inner Classes

  • Declared in the class
  • visibility decides on access control outside
  • f the class

public class HelloWorld extends Object{ class Hello2 extends HelloWorld{ }; // method declaration public void wave(){ ... } } ... HelloWorld.Hello2 h=new HelloWorld.Hello2();

64

slide-12
SLIDE 12

Some interesting classes

  • Object
  • String
  • System
  • Vector

65

slide-13
SLIDE 13

Object

  • See API

66

slide-14
SLIDE 14

String

  • See API

67

slide-15
SLIDE 15

System

  • See APIs

68

slide-16
SLIDE 16

Vector

  • See APIs

69

slide-17
SLIDE 17

Java Basics Part 3 - Exceptions

Manuel Oriol, April 13th, 2006

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 21

Error

  • Meant to represent an unrecoverable error
  • Can be recovered still...
  • Example: AnnotationFormatError, AssertionError,

AWTError, CoderMalfunctionError, FactoryConfigurationError, LinkageError, ThreadDeath, TransformerFactoryConfigurationError, VirtualMachineError

74

slide-22
SLIDE 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

slide-23
SLIDE 23

try...catch... finally

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

76