Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

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

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X 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

3 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 SortedList class Problem  It inherits two methods which, if used could destroy the order of the entries in a sorted list

4 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

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

6 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

7 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

8 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

9 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 changescode samples

10 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

11 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 LinkedChainBaseView code Note next iteration of LListRevised which extends LinkedChainBasenext iteration

12 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 > extends LinkedChainBase implements SortedListInterface, java.io.Serializable

13 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 methodrevised add  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 getNodeBeforemethod getNodeBefore

14 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(n 2 ) 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


Download ppt "Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google