Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides by Steve Armstrong LeTourneau University Longview, TX

Similar presentations


Presentation on theme: "Slides by Steve Armstrong LeTourneau University Longview, TX"— Presentation transcript:

1 Slides by Steve Armstrong LeTourneau University Longview, TX
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX ã 2007, Prentice Hall

2 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 X

3 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 X

4 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 X

5 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 X

6 Designing a Base Class Fig 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 X

7 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 X

8 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 X

9 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 X

10 Designing a Base Class Fig 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 X

11 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 X

12 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 X

13 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 X

14 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 X


Download ppt "Slides by Steve Armstrong LeTourneau University Longview, TX"

Similar presentations


Ads by Google