Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
56 slides
-
Memory Upload • Superclass Item and subclass ComparableItem • Implementing
interface Comparable for subclass ComparableItem • Superclass LinkedList and subclass OrderedLinkedList • Adding grocery items in alphabetical order for the GroceryMenu program -
Alphabetically Ordered List • Your boss wants you to improve
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 -
Inheritance • Inheritance is a way for a class to
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 -
Superclass and Subclass • Superclass (base class) is the class
from which other classes will be built • Subclass (derived class) is a new class built from an existing class -
Superclass and Subclass • The subclass inherits (can use) the
data fields and methods of its superclass • A subclass can also create its own data fields and methods, which do not exist in the superclass -
First Step • We first need to change the Item
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 -
Subclass Declaration • A subclass must inherit from a superclass
• 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 Constructor • Must call the superclass’s constructor from the
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(); -
Example Code • Class ComparableItem is composed of the same
two data fields as Item, since it inherits all the data fields from Item private String name; private int count; -
ComparableItem Object • Memory representation of Item object Item item1
= new Item("apples", 5); item1 apples 5 name number -
ComparableItem Object • A ComparableItem object has the same structure
as an Item object ComparableItem item2 = new ComparableItem("bananas", 7); item2 bananas 7 name number -
Interface • An interface is a list of method prototypes
(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 -
Interface Comparable • For example, interface Comparable has this code
public interface Comparable{ public int compareTo(Object object)throws ClassCastException; } -
Interface Comparable • Interface Comparable is useful whenever we want
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 -
Interface Comparable • According to Java API, the method int
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.” -
Class ComparableItem • Implement interface Comparable using class ComparableItem as
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 -
Class ComparableItem • Make sure that the second item being
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 -
Overriding • Overriding is when a subclass redefines (rewrites the
code to) a method of its superclass • We use overriding to create a method that is more appropriate for the implementation of the subclass -
Overriding • Overriding takes place when the methods of the
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 -
Overriding Example • Superclass: LinkedList.java • Subclass: OrderedLinkedList.java • Methods
that use overriding • add() • Methods that are inherited from superclass LinkedList • remove(), get(), toString(), getSize() -
Algorithm for Ordered Add 1. If list is empty, add
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 -
Algorithm for Ordered Add 3.Else add to middle or end
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 -
Big-O for Ordered Add • Because the add() method adds
items in order, we will add items, on average, to the middle of the list • Therefore, the Big-O is O(n/2) = O(n) -
Ordered add() Method • Add string "E" to empty ordered
list ListInterface<String> stringList = new OrderedLinkedList<String>(); list.add("E"); list 0 head size null -
Ordered add() Method • Program control jumps to add() method
of class OrderedLinkedList public void add(T item){ item list 0 head size null E -
Ordered add() Method • Program control jumps to add() method
of class OrderedLinkedList if (head == null){ head = new Node<T>(item, null); } item list 0 head size E data next null -
Ordered add() Method • Increment list size size++; item list
1 head size E data next null -
Ordered add() Method • Finished method definition, so return to
line after the method call list.add("E"); list.add("B"); list 1 head size E data next null -
Ordered add() Method • Next line of code is to
add string "B" to the ordered linked list list.add("B"); list 1 head size E data next null -
Ordered add() Method • Jump to method definition of the
ordered add() method public void add(T item){ item list 1 head size E data next null B -
Ordered add() Method • Add to front of list if(item.compareTo(head.getData())<=0)
{ head = new Node<T>(item, head); } item list 1 head size data next null B data next E -
Ordered add() Method • Add 1 to size of list
size++; item list 2 head size data next null B data next E -
Ordered add() Method • End of method definition, so return
to main() method to next line of code list.add("B"); list.add("C"); list.add("A"); list 2 head size data next null B data next E -
Ordered add() Method • Strings "E", "B", "C", and "A"
have alredy been added to an ordered list list.add("D"); list 4 head size A data next data next B C data next E data next null -
Ordered add() Method public void add(T item){ item list 4
head size A data next data next B C data next E data next null D -
Ordered add() Method Node<T> previous = head; item previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method Node<T> current = head.getNext(); item current previous
list 4 head size A data next data next B C data next E data next null D -
Ordered add() Method while(current!=null && item.compareTo(current.getData())>0){ item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method previous = current; item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method current = current.getNext(); item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method while(current!=null && item.compareTo(current.getData())>0){ item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method previous = current; item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method current = current.getNext(); item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method while(current!=null && item.compareTo(current.getData())>0){ item current previous list
4 head size A data next data next B C data next E data next null D -
Ordered add() Method Node<T> node=new Node<T>(item,current); node item current previous
list 4 head size A data next data next B C data next E data next null D data next -
Ordered add() Method previous.setNext(node); node item current previous list 4
head size A data next data next B C data next E data next null D data next -
Ordered add() Method size++; node item list 5 head size
A data next data next B C data next E data next null D data next -
Ordered add() Method • Method done list 5 head size
A data next data next B C data next E data next null D data next -
Access Modifier • 3 kinds of access modifiers 1.public: visible
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 -
LinkedList Class • The data fields of the LinkedList superclass
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); -
OrderedLinkedList Class • Generic class T needs to be a
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>{ -
OrderedLinkedList Class • Your code must to indicate that the
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> { -
Changes to GroceryMenu • We also need to change the
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 -
Memory Defragmenter • Inheritance with superclass Item and subclass ComparableItem
• Implemented a compareTo() method for interface Comparable • Inheritance for superclass LinkedList and subclass OrderedLinkedList • Changed GroceryMenu program to add items in alphabetical order -
Task Manager • Before the next class, you need to:
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