180 likes | 360 Views
More Java: Static and Final, Abstract Class and Interface, Exceptions , Collections Framework . Abstract class and interface. Abstract class : Declaration omitting implementation Interface : a "contract" that spells out how software interacts .
E N D
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework CS300
Abstract class and interface • Abstract class: Declaration omitting implementation • Interface: a "contract" that spells out how software interacts. • Example: sort contacts on your phone based on e-mail addresses CS300
Static and Final • Final declaration cannot be changed • Classes, methods, fields, parameters and local variables can be final • Static: belongs to the class in which it is described, not an instance of a class • Example: • Download QuietStatic.java and LoudStatic.java • Create StaticClientfor QuietStatic where you increment class member and instance member • Fill in LoudStatic CS300
Exceptions • Handle unusual conditions CS300
Collection framework • One of the most powerful and convenient tools of Java • java.util package: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html CS300
Collection interface types • Collection: root type. Group of objects, not necessarily ordered, not necessarily addressable, possibly containing duplicates. You can: • Add/remove things from it • Get its size • Iterate over it CS300
Collection interface types • List: ordered collection, index from 0 to length -1. May contain duplicates. You can: • Add/remove things from it • Get its size • Iterate over it • Map an element to its index • Map an index to an element • Change an element at a specific index CS300
Collection interface types • Set: unordered collection. Does not contain duplicates. You can: • Add/remove things from it • Get its size • Iterate over it • Map: like a list. Instead of mapping a collection of objects to integer indexes, it maps them to a set of key objects • Examples:mapping of words to their definitions, dates to events, or URLs to cached content CS300
Collection interface types • What you can do with a Map: • Add/remove things from it • Get its size • Iterate over it • Map an element to its key • Map keyto an element • Change an element at a specific key CS300
Collection interface types • Iterator: Returns elements from a collection from which it is derived, exactly once, in response to calls to its next method. CS300
Collection implementation types • ArrayList: list implemented by an array. • Quick to index • Slow to change size • LinkedList: list that can change size quickly but it is slower to index. CS300
Collection implementation types • HashSet: set that is implemented as a hash.add, remove, contains, and size all execute at constant time assuming well behaved hash. May contain no more than one null. • HashMap: implementation of Map that uses hash table as its index. add, remove, contains, and size all execute at constant time assuming well behaved hash. May contain a single null key but any number of values may be null. CS300
Collection implementation types • TreeMap: orderedMap, objects in the map are sorted according to their natural order, or according to Comparator. CS300
Java Generics CS300
References • Programming Android by ZigurdMednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura • Exceptions http://docs.oracle.com/javase/6/docs/api/java/lang/Exception.html CS300