12 - 18:08:41 CS 61B Lecture 12 Wednesday Todays reading Sierra Bates Chapter 8 12 1 public void listSort(List l cfw ABSTRACT CLASSES = An abstract

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

View Full Document Right Arrow Icon
02/16/09 18:08:41 1 12 CS 61B: Lecture 12 Wednesday, February 18, 2009 Today’s reading: ABSTRACT CLASSES ================ An abstract class is a class whose sole purpose is to be extended. public abstract class List { protected int size; public int length() { return size; } public abstract void insertFront(Object item); } Abstract classes don’t allow you to create objects directly. You can declare a variable of type List, but you can’t create a List object. List myList; // Right on. myList = new List(); // COMPILE-TIME ERROR. However, abstract classes can be extended in the same way as ordinary classes, and the subclasses are usually not abstract. (They can be, but usually they’re normal subclasses with complete implementations.) The abstract List class above includes an abstract method, insertFront. An abstract method lacks an implementation. The purpose of an abstract method is to guarantee that any non-abstract subclass will implement the method. Specifically, any non-abstract subclass of List must have an implementation for the insertFront method. public class SList extends List { // inherits the "size" field. protected SListNode head; // inherits the "length" method. public void insertFront(Object item) { head = new SListNode(item, head); size++; } } If you were to leave out the implementation of insertFront in SList, the Java compiler would complain that you must provide one. A non-abstract class may never contain an abstract method, nor inherit one without providing an implementation. Because SList is not abstract, we can create SList objects; and because SLists
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

12 - 18:08:41 CS 61B Lecture 12 Wednesday Todays reading Sierra Bates Chapter 8 12 1 public void listSort(List l cfw ABSTRACT CLASSES = An abstract

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