11 - 02/16/09 18:36:38 CS 61B: Lecture 11 Friday, February...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
02/16/09 18:36:38 1 11 CS 61B: Lecture 11 Friday, February 13, 2009 Today’s reading: All of Chapter 7, plus pp. 28-33, 250-257. INHERITANCE =========== In Lab 3, you modified several methods in the SList class so that a "tail" reference could keep track of the end of the list, thereby speeding up the insertEnd() method. We could have accomplished the same result without modifying SList--by creating a new class that inherits all the properties of SList, and then changing only the methods that need to change. Let’s create a new class called TailList that inherits the fields and methods of the original SList class. public class TailList extends SList { /* The "head" and "size" fields are inherited from SList. */ private SListNode tail; This code declares a TailList class that behaves just like the SList class, but has an additional field "tail" not present in the SList class. TailList is said to be a _subclass_ of SList, and SList is the _superclass_ of TailList. A TailList has three fields: head, size, and tail. A subclass can modify or augment a superclass in at least three ways: (1) It can declare new fields. (2) It can declare new methods. (3) It can override old methods with new implementations. We’ve already seen an example of the first. Let’s try out the third. The advantage of TailList is that it can perform the insertEnd() method much more quickly than a tail-less SList can. So, let’s write a new insertEnd() for TailList, which will _override_ SList’s old, slow insertEnd() method. public void insertEnd(Object obj) { /* Your solution to Lab 3 goes here. */ } The isEmpty(), length(), nth(), and toString() methods of SList do not need any changes on account of the tail reference. These methods are inherited from SList, and there’s no need to rewrite them. Inheritance and Constructors ---------------------------- What happens when we construct a TailList? As you would expect, Java executes a TailList constructor, but first it executes the code in the SList() constructor. The TailList constructor should initialize fields unique to TailList. It can also modify the work done by SList() if appropriate. public TailList() { // SList() constructor called automatically; sets size = 0, head = null tail = null; } The zero-parameter SList() constructor is always called by default, regardless of the parameters passed to the TailList constructor. To change this default
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at University of California, Berkeley.

Page1 / 2

11 - 02/16/09 18:36:38 CS 61B: Lecture 11 Friday, February...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online