Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.

Similar presentations


Presentation on theme: "More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300."— Presentation transcript:

1 More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300

2 Abstract class and interface  Abstract class: Declaration omitting implementation Abstract class  Interface: a "contract" that spells out how software interacts. Interface  Example: sort contacts on your phone based on e- mail addresses CS300 2

3 Java vs C#: Classes / Interfaces 3 JavaC# Accessibility keywords public private protected static // Inheritance class FootballGame extends Competition {... } Accessibility keywords public private internal protected protected internal static // Inheritance class FootballGame : Competition {... } CS300

4 Java vs C#: Classes / Interfaces 4 JavaC# // Interface definition interface IAlarmClock {... } // Extending an interface interface IAlarmClock extends IClock {... } // Interface implementation class WristWatch implements IAlarmClock, ITimer {... } // Interface definition interface IAlarmClock {... } // Extending an interface interface IAlarmClock : IClock {... } // Interface implementation class WristWatch : IAlarmClock, ITimer {... } CS300

5 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 StaticClient for QuietStatic where you increment class member and instance member Fill in LoudStatic CS300 5

6 Exceptions  Handle unusual conditions CS300 6

7 Java vs C#: Exception Handling 7 JavaC# // Must be in a method that is declared to throw this exception Exception ex = new Exception("Something is really wrong."); throw ex; try { y = 0; x = 10 / y; } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { // Code that always gets executed } Exception up = new Exception("Something is really wrong."); throw up; // ha ha try { y = 0; x = 10 / y; } catch (Exception ex) { // Variable "ex" is optional Console.WriteLine(ex.Message); } finally { // Code that always gets executed } CS300

8 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/Collectio ns.html http://docs.oracle.com/javase/6/docs/api/java/util/Collectio ns.html CS300 8

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

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

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

12 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 key to an element  Change an element at a specific key CS300 12

13 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 13 Instead of:We prefer: for(int i = 0; i < list.size(); i++) { String s = list.get(i); //… } for(Iterator i = list.iterator; i.hasNext(); ) { String s = i.next(); //… } Or for(String s : list){ //… }

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

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

16 Collection implementation types  TreeMap: ordered Map, objects in the map are sorted according to their natural order, or according to Comparator. CS300 16

17 Java Generics Without Generics:With Generics: public List makeList(){ // … } public void useList(List l){ Thing t = (Thing) l.get(0); // … } //… useList(makeList()); // useList has no guarantee that makeList created a list of Thing, compiler cannot verify that the cast in useList will work and the code might explode at runtime public List makeList(){ // … } public void useList(List l){ Thing t = (Thing) l.get(0); // … } //… useList(makeList()); CS300 17

18 References  Programming Android by Zigurd Mednieks, Laird Dornin, G. Blake Meike, Masumi Nakamura  Exceptions http://docs.oracle.com/javase/6/docs/api/java/la ng/Exception.html CS300 18


Download ppt "More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300."

Similar presentations


Ads by Google