Download Ch14v2.0

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Inheritance and
Lists
Chapter 14
Slides by Steve Armstrong
LeTourneau University
Longview, TX
2007, Prentice Hall
Chapter Contents
• Using Inheritance to Implement a
Sorted List
• Designing a Base Class
 Creating an Abstract Base Class
• An Efficient Implementation of a
Sorted List
 The Method add
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Using Inheritance to Implement a
Sorted List
• View revised SortedList class which
inherits from LList
• Problem
 It inherits two methods which, if used could
destroy the order of the entries in a sorted list
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Using Inheritance to Implement a
Sorted List
• Possible solutions
 Declare sorted list as an instance of
SortedListInterface
 Implement the add and replace within
SortedList, but have them return false
 Implement them but have them throw an
exception
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Designing a Base Class
• Note excerpt of class LList
• We want LList as base class for further
development
• Efficient extension will require subclass
access to
 Data field firstNode
 Method getNodeAt
 Class Node
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Designing a Base Class
Fig. 14-1 A derived class of the class LList cannot access
or change anything that is private within LList
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Protected Access
• You can access a protected method
or data field of a given class by
name only …
 Within its own class definition
 Within a class derived from that class
 Within any class in the same package
as that class
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Changes to LList
• Declare firstNode and length to be
protected
• Provide protected methods




addFirstNode
addAfterNode
removeFirstNode
removeAfterNode
• Make getNodeAt protected
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Changes to LList
• Make class Node and its constructors
protected
• Add protected methods




setData
getData
setNextNode
getNextNode
• View code samples of changes
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Designing a Base Class
Fig. 14-2 Access
available to a class
derived from the class
LinkedListRevised
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Creating an Abstract Base Class
• Simplify class LListRevised
 Note portion that deals with chain of linked
nodes
 Place that portion into an abstract base class
• View code for LinkedChainBase
• Note next iteration of LListRevised
which extends LinkedChainBase
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficient Implementation of a Sorted
List
• The class LinkedListBase enables
faster manipulation of the list's underlying
data structure
• We want the class to extend
LinkedChainBase
public class SortedLinkedList
< T extends Comparable < ? super T >>
extends LinkedChainBase < T >
implements SortedListInterface < T > ,
java.io.Serializable
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
New and Revised Methods
• Note revised add method
 Note differences from add method in
Segment 13.10
 Proceeding protected methods with
super is
optional
 No other methods have their methods
• View private method getNodeBefore
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Efficiency
• getPosition is O(n2)
• Improved add method is O(n)
• The class designer should use
inheritance and maintain efficiency
 Requires that base class provided
protected access to underlying data
structure
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X