Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Overview • Java language inheritance and composition interfaces • Case study: reusable list Object-Oriented Design in Java Java collections • Case study William Marsh AWT Listeners • Design guidelines Composition and Inheritance Account Java Classes Transaction Deposit • ‘Account’ has-a ‘Transaction’ • ‘Deposit’ is-a ‘Transaction’ • ‘Withdraw’ is-a ‘Transaction’ Withdraw Overriding Methods Widening Transaction • Alternative implementation of method of the superclass • • • • toString() same name same number of parameters same parameter types and same return types • If parameters are different, the method is overloaded rather than overridden Deposit Withdraw toString() toString() • Every subclass object is also a superclass object Deposit d = ... ; Transaction t ; t = d ; Transaction toString() Deposit Withdraw toString() toString() Every ‘Deposit’ is also a ‘Transaction’ Narrowing Quiz 1 • What about: • How to prevent a method being over-ridden? • How to require a method to be over-ridden? • How to call method from the super class? Deposit d ; Transaction t = ... ; d = t; compilation error Not all transactions are deposits! Deposit d ; Transaction t = ... ; d = (Deposit) t; down cast Quiz 2 - Result of this Programme? class A { int x ; A(int z) { x = z; } int m(int y){ return x + y ; } } class B extends A { B(int z) { super(z) } int m(int y){ return x - y ; } } A o = new A(1); System.out.println(o.m(1)); Quiz 4 - Result of this Programme? class A { int x ; A(int z) { x = z; } int m(int y){ return x + y ; } } class B extends A { B(int z) { super(z) } int m(int y){ return x - y ; } } A o = new B(1); System.out.println(o.m(1)); Quiz 3 - Result of this Programme? class A { int x ; A(int z) { x = z; } int m(int y){ return x + y ; } } class B extends A { B(int z) { super(z) } int m(int y){ return x - y ; } } B o = new B(1); System.out.println(o.m(1)); Interfaces • An ‘interface’ is similar to a class methods but no attributes signature but no implementation • A ‘contract’ • Class ‘C’ implements interface ‘I’ class must have both methods class C implements I { void m1() { ... } void m2() { ... } } «interface» I m1() m2() C m1() m2() Interfaces as Types - ‘Known As’ Interface v. Abstract Class • C can be ‘known as’ I • Class can implement multiple interfaces class C implements I { void m1() { ... } void m2() { ... } } «interface» I m1() m2() C C cVar = new C() ; I iVar = cVar ; «interface» I m1() m2() • Abstract class may contain implementation «interface» J m3() m4() C m1() m2() m1() m2() m3() m4() List of Transactions? Account addTransaction() Case Study - Reusable Lists first Transaction getNext() setNext() next • List code integrated with application code • No reuse • No abstraction Reusable List List Interface • Abstraction ‘list’ is an abstraction: head, tail, append more than one list implementation need an interface • Methods to provide the ‘list abstraction’ List public interface List { public int size() ; public boolean isEmpty() ; public Object head() ; public void append(Object item) ; public Object removeHead() ; } LinkedList • Reuse ArrayList class for each implementation List Implementation Polymorphism • ‘Many forms’ • List should be polymorphic public class LinkedList implements List { public public public public public element of any type • Java solution: elements have type Object polymorphism by inheritance int size() { ... } boolean isEmpty() { ... } Object head() { ... } void append(Object item) { ... } Object removeHead() { ... } } Quiz 5 • How to hold an integer in a list? basic types are not objects Quiz 6 • Implement ‘size’ and ‘isEmpty’ Linked List Implementation Linked List Implementation public class LinkedList implements List { protected int count ; protected Node first ; public class LinkedList implements List { protected int count ; protected Node first ; public int size() { return count ; } public boolean isEmpty() { return Node == null ; } /* what is Node ? */ } } Linked List Implementation Linked List Implementation public class LinkedList implements List { protected int count ; protected Node first ; ... static protected class Node { Object element ; Node next ; } public class LinkedList implements List { public Object head() { ObjectElem head ; if (first != null) { head = first.element ; } return head ; } public void append(Object item) { ... } public Object removeHead() { ... } Quiz 7 public Object head() { ... } public void append(Object item) { ... } public Object removeHead() { ... } } public int size() { return count ; } public boolean isEmpty() { return Node == null ; } static protected class Node { Object element ; Node next ; } nested class } Using the List – A Price to Pay class Account { List transactions ; /* what is in the list? */ printTransactionsAmounts() { ... Quiz 8 } } Using the List List trans ; /* list of transactions */ printTransactionsAmounts() { while (! trans.isEmpty() ) { Transaction t = (Transaction) trans.removeHead() ; System.out.println(t.getAmount()) ; } } down cast • List contains ‘Object’ - only the variable name suggests content • Program to the ‘List’ interface not implementation • WHOOPS - list has been destroyed Iterating Through the List Iteration Methods List trans ; /* list of transactions */ printTransactionsAmounts() { Node n = trans.first ; while ( node != null ) { Transaction t = (Transaction) node.element ; System.out.println(t.getAmount()) ; } } • Problem - node class must be visible • How to add iteration to the interface? • Current position in the list ‘cursor’ void reset() Move to the start of the list Object next() Return the element in the current position and advance the position boolean hasNext() Return true if not at the end Quiz 9 – Using ‘Iteration Methods’ Using ‘Iteration Methods’ • Rewrite printTransactionAmounts • Rewrite printTransactionAmounts List trans ; /* list of transactions */ List trans ; /* list of transactions */ printTransactionsAmounts() { printTransactionsAmounts() { trans.reset() ; while ( trans.hasNext() ) { Transaction t = (Transaction) trans.next() ; System.out.println(t.getAmount()) ; } } ... } Idea 1 – Add ‘Iteration Methods’ to List Interface Idea 2 – Separate ‘Iterator Interface’ protected Node cursor ; void reset() { cursor = first ; } boolean hasNext() { return (cursor != null) ; } Object next() { Node n ; if (cursor != null) { n = cursor.element ; cursor = cursor.next ; } return n ; } public interface Iterator { void reset() ; boolean hasNext() ; Object next() ; } Iterator • ‘LinkedList’ implements ‘List’ and ‘Iterator’ LinkedList List Ideas 1 & 2 – Problem Idea 3 – Separate the Iterator Iterator • Problem List List Iterator LinkedList LinkedListIterator only one iterator may need more LinkedList • List can have many iterators • ‘List’ interface includes method to create an iterator public interface List { ... public Iterator iterator() ; } Java Collection Classes • Good news! difficult algorithms implemented for you • Interfaces Java Collections Collection Map Java provides a set of ‘collection’ classes; what are they for and how are they used? SortedSet Set List SortedMap Java Collection Classes More Topics • Good news! • ‘Map’ interface and implementations difficult algorithms implemented for you • Implementations finite functions • How sorted sets and map are handled • Lab sheet 3 – ‘Using Java Collections’ Collection Set TreeSet List HashSet LinkedList ArrayList Listener Interfaces • How to a user action (e.g. ‘button press’) to code? ‘Listener’ Interfaces in AWT/Swing public interface ActionListener { public void ActionPerformed ( ActionEvent e) ; } • Create an implementation of the listener • Add it to the component void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this button. Listener Interfaces • Different listeners for each interaction actions mouse clicks and movements .... • Listeners often implemented as inner-classes • See Lab sheet 2 Summary Design Guidelines Summary • Separate interface from implementation • Java language interfaces inner classes • ... used to design reusable software Java library YOUR SOFTWARE Reading • General Jia chapters 5 & 6 Bennet et al. chapters 11 & 13 • Collections Jia section 8.2 • AWT/Swing Jia section 8.3 • On the web Collections http://java.sun.com/docs/books/tutorial/collections/index.html Article on interfaces and abstract classes http://www.javaworld.com/javaworld/jw-09-2001/jw-0921-interface_p.html Article on interfaces and composition http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html