interface Comparable for subclass ComparableItem • Superclass LinkedList and subclass OrderedLinkedList • Adding grocery items in alphabetical order for the GroceryMenu program
the grocery list • She wants the items in the grocery list to be alphabetically ordered • Instead of starting from the beginning again, you can be “lazy” and take advantage of code you have already written by using inheritance
build on top of the data fields and methods of an already existing class • When we use inheritance, we do NOT have to “reinvent the wheel” (start from the beginning) when creating a new class
class, so that Item objects can be put into alphabetical order • Instead of changing code that already works (and risk creating bugs), we create subclass ComparableItem (from superclass Item) which has a compareTo() method to put items into alphabetical order
• Use keyword “extends” and the superclass’s name at the beginning of the class declaration public class ComparableItem extends Item{ • See files Item.java and ComparableItem.java for the example code
subclass’s constructor definition • Use the keyword “super” to call superclass’s constructor super(name, number); • If not done, compiler will automatically call this constructor super();
(first line of a method) • When a class implements a particular interface, this ensures that designers of the class and users of the class are using the same method names, parameters types and return types
to arrange items in a particular order • The class that implements interface Comparable must have the compareTo() method • See Java API for details on interface java.lang.Comparable
compareTo(Object o) is implemented as follows • “Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.”
a parameter public class ComparableItem extends Item implements java.lang.Comparable <ComparableItem>{. . . } • This is done, so that the type can be checked during compile time, rather than waiting until runtime to possibly throw an exception
compared, which is the parameter of the compareTo() method, is same type as the generic type of the class public int compareTo(ComparableItem item2){. . .} • See ComparableItem.java for more details on code for compareTo() method
superclass and subclass have the same method prototype • In other words, the first line of both methods in both classes is the same, but the code in the body of the two methods is different
new node to beginning of list: a. Assign head the address of the new node 2. If new item comes before the first item, add new node to beginning: a. Create new node that points to first node b. Point head to new node
of list: a. Point previous to first node b. Point current to second node c. WHILE current is not null AND item is larger than current item I. Assign previous to current node II.Assign current to next node d. Make new node point to current node e. Point previous node to the new node 4.Add 1 to size of list
to class and its subclasses and client programs 2.protected: visible only to class and its subclasses and any other classes in the same project (folder) 3.private: visible only within the class
must have protected access • This is so that the add() method of subclass OrderedLinkedList can access these data fields protected Node<T> head = null; protected Integer size = new Integer(0);
class that implements the Comparable interface, so we need to use keyword extends to indicate generic class T must implement a specific interface public class OrderedLinkedList<T extends java.lang.Comparable<T>> extends LinkedList<T>{
superclass LinkedList will use the same generic class T as the subclass, so you have to put <T> after the superclass’s name public class OrderedLinkedList<T extends java.lang.Comparable<T>> extends LinkedList<T> {
class names in the GroceryMenu class • From LinkedList to OrderedLinkedList • From Item to ComparableItem • See GroceryMenu2.java code for the new program With only a few changes, our program adds items alphabetically to the list
• Implemented a compareTo() method for interface Comparable • Inheritance for superclass LinkedList and subclass OrderedLinkedList • Changed GroceryMenu program to add items in alphabetical order
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture