Data Structures and Abstractions with Java 5 th
Data Structures and Abstractions with Java™ 5 th Edition Chapter 18 Inheritance and Lists Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inheritance to Implement a Sorted List /** A class that implements the ADT sorted list by extending LList. Duplicate entries are allowed. */ public class Sorted. List<T extends Comparable<? super T>> extends LList<T> implements Sorted. List. Interface<T> { public void add(T new. Entry) { int new. Position = Math. abs(get. Position(new. Entry)); super. add(new. Position, new. Entry); } // end add /* < Implementations of remove(an. Entry) and get. Position(an. Entry) go here. >. . . */ } // end Sorted. List If Sorted. List inherited methods from LList, we would not have to implement them again. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inheritance to Implement a Sorted List /** Adds new. Entry to the list at position new. Position. */ public void add(int new. Position, T new. Entry); /** Replaces the entry at given. Position with new. Entry. */ public T replace(int given. Position, T new. Entry); Although Sorted. List conveniently inherits methods such as is. Empty from LList, it also inherits two methods that a client can use to destroy the order of a sorted list. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Inheritance to Implement a Sorted List • Possible ways to avoid the pitfall – Use Sorted. List. Interface in the declaration of the sorted list. – Implement the list’s add and replace methods within the class Sorted. List and have them throw an exception when invoked Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class (Part 1) /** A class that implements the ADT list by using a chain of linked nodes that has a head reference. */ public class LList<T> implements List. Interface<T> { private Node first. Node; // Reference to first node private int number. Of. Entries; public LList() { initialize. Data. Fields(); } // end default constructor public int clear() { initialize. Data. Fields(); } // end clear /* < Implementations of the public methods add, remove, replace, get. Entry, contains, get. Length, is. Empty, and to. Array go here. >. . . */ LISTING 18 -1 Relevant aspects of the class LList Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class (Part 2) // Initializes the class’s data fields to indicate an empty list. private void initialize. Data. Fields() { first. Node = null; number. Of. Entries = 0; } // end initialize. Data. Fields // Returns a reference to the node at a given position. private Node get. Node. At(int given. Position) { //. . . } // end get. Node. At private class Node { private T data; private Node next; //. . . } // end Node } // end LList LISTING 18 -1 Relevant aspects of the class LList Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class LList Private data fields: first. Node number. Of. Entries Private methods: initialize. Data. Fields get. Node. At Private inner class: Node • • • Any subclass (derived class) Cannot access or change first. Node Cannot change number. Of. Entries Cannot invoke initialize. Data. Fields Cannot invoke get. Node. At Cannot create an instance of Node Cannot access or change fields of an existing node FIGURE 18 -1 A derived class of the class LList cannot access or change anything that is private within LList Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class protected final Node get. First. Node() { return first. Node; } // end get. First. Node Protected method get. First. Node, enabling the subclass to access the head reference first. Node Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class /** Adds a node to the beginning of a chain. */ protected final void add. First. Node(Node the. Node) /** Adds a node to a chain after a given node. */ protected final void add. After. Node(Node node. Before, Node the. Node) /** Removes a chain’s first node. */ protected final T remove. First. Node() /** Removes the node after a given one. */ protected final T remove. After. Node(Node node. Before) Protected methods to add and remove nodes, changing first. Node and number. Of. Entries as necessary Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class /** Adds a node to the beginning of a chain. */ protected final void add. First. Node(Node the. Node) { // Assertion: the. Node != null the. Node. set. Next. Node(first. Node); first. Node = the. Node; number. Of. Entries++; } // end add. First. Node Definition of add. First. Node. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class public T remove(int given. Position) { T result = null; if ((given. Position >= 1) && (given. Position <= get. Length())) { // Assertion: The list is not empty if (given. Position == 1) // Case 1: Remove first entry result = remove. First. Node(); else // Case 2: given. Position > 1 { Node node. Before = get. Node. At(given. Position - 1); result = remove. After. Node(node. Before); } // end if return result; // Return removed entry } else throw new Index. Out. Of. Bounds. Exception( “Illegal position given to remove operation. "); } // end remove Revision of LList’s method remove Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class protected final T get. Data() protected final void set. Data(T new. Data) protected final Node get. Next. Node() private final void set. Next. Node(Node next. Node) Node has these four methods. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
LList. Revised Private data fields: first. Node number. Of. Entries Protected, final methods: get. First. Node add. After. Node remove. First. Node remove. After. Node get. Node. At initialize. Data. Fields Designing a Base Class Protected, final inner class: Node (Has protected methods get. Data, set. Data, and get. Next. Node and private method set. Next. Node) FIGURE 18 -2 Access available to a class derived from the class LList. Revised Any subclass (derived class) • • • Can access first. Node and number. Of. Entries Can invoke the protected methods Can create an instance of Node Can access the fields of an existing node Can change the data in an existing node Cannot override Node and the final methods Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Designing a Base Class Linked. Chain. Base Private data fields: first. Node number. Of. Entries Public methods: clear get. Length is. Empty to. Array Protected, final methods: get. First. Node add. After. Node remove. First. Node remove. After. Node get. Node. At initialize. Data. Fields Protected, final inner class: Node (Has protected methods get. Data, set. Data, and get. Next. Node and private method set. Next. Node) FIGURE 18 -3 The separation of linkedchain operations and list operations Linked. Chain. List Public methods: add remove replace get. Entry contains Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Creating an Abstract Base Class /** An abstract base class for use in implementing the ADT list by using a chain of nodes. All methods are implemented, but since the class is abstract, no instances can be created. */ public abstract class Linked. Chain. Base<T> { private Node first. Node; // Reference to first node private int number. Of. Entries; public Linked. Chain. Base() { initialize. Data. Fields(); } // end default constructor /* < Implementations of the public methods clear, get. Length, is. Empty, and to. Array go here. >. . . < Implementations of the protected, final methods get. First. Node, add. After. Node, remove. First. Node, remove. After. Node, get. Node. At, and initialize. Data. Fields go here. > /* protected final class Node { < Implementations of the protected methods get. Data, set. Data, and get. Next. Node go here. > < Implementation of the private method set. Next. Node goes here. > } // end Node } // end Linked. Chain. Base . . . */ LISTING 18 -2 The abstract base class Linked. Chain. Base Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Efficient Implementation of a Sorted List public class Linked. Chain. Sorted. List<T extends Comparable<? super T>> extends Linked. Chain. Base<T> implements Sorted. List. Interface<T> We want our class to extend Linked. Chain. Base Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Efficient Implementation of a Sorted List public void add(T new. Entry) { Node the. Node = new Node(new. Entry); Node node. Before = get. Node. Before(new. Entry); if (node. Before == null) // No need to call is. Empty add. First. Node(the. Node); else add. After. Node(node. Before, the. Node); } // end add Details of a previous add method now hidden within the protected methods add. First. Node and add. After. Node of Linked. Chain. Base Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Efficient Implementation of a Sorted List private Node get. Node. Before(T an. Entry) { Node current. Node = get. First. Node(); Node node. Before = null; while ((current. Node != null) && (an. Entry. compare. To(current. Node. get. Data()) > 0)) { node. Before = current. Node; current. Node = current. Node. get. Next. Node(); } // end while return node. Before; } // end get. Node. Before The private method get. Node. Before Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
End Chapter 18 Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
- Slides: 19