12 - 21:29:17 CS 61B Lecture 12 Friday Todays reading 1 12...

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

View Full Document Right Arrow Icon
09/22/10 21:29:17 1 12 CS 61B: Lecture 12 Friday, September 24, 2010 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 01/10/2012 for the course CS 61B taught by Professor Canny during the Fall '01 term at Berkeley.

Page1 / 2

12 - 21:29:17 CS 61B Lecture 12 Friday Todays reading 1 12...

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