DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

02/15/1405:35:51 112 CS 61B: Lecture 12 Wednesday, February 19, 2014Today’s reading: Sierra & Bates, Chapter 8.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 avariable 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’renormal subclasses with complete implementations.)The abstract List class above includes an abstract method, insertFront. Anabstract method lacks an implementation. One purpose of an abstract method isto guarantee that every non-abstract subclass will implement the method.Specifically, every non-abstract subclass of List must have an implementationfor 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 leave out the implementation of insertFront in SList, the Java compilerwill complain that you must provide one. A non-abstract class may nevercontain an abstract method, nor inherit one without providing animplementation.Because SList is not abstract, we can create SList objects; and because SListsare Lists, we can assign an SList to a List variable. List myList = new SList(); // Right on. myList.insertFront(obj); // Right on.What are abstract classes good for? It’s all about the interface. ---------------------------------------------------- | An abstract class lets you define an interface | | - for multiple classes to share, | | - without defining any of them yet. | ----------------------------------------------------Let’s consider the List class. Although the List class is abstract, it is anADT--even without any implementation!-- because it has an interface with publicmethod prototypes and well-defined behaviors. We can implement analgorithm--for example, a list sorter--based on the List interface, withoutever knowing how the lists will be implemented. One list sorter can sort everykind of List. public void listSort(List l) { ... }In another part of the universe, your project partners can build lots ofsubclasses of List: SList, DList, TailList, and so on. They can also buildspecial-case List subclasses: for example, a TimedList that records the amountof time spent doing List operations, and a TransactionList that logs allchanges made to the list on a disk so that all information can be recovered ifa power outage occurs. A library catalogue application that uses DLists cansend them to your listSort algorithm to be sorted. An airline flight databasethat uses TransactionLists can send them to you for sorting, too, and you don’thave to change a line of sorting code. You may have written your list sorteryears before TransactionLists were ever thought of. ----------------- The list sorter is built on the foundation of a list | Application | ADT, and the application is built on the foundation of ----------------- the list sorter. However, it’s the application, and | not the list sorter, that gets to choose what kind of | calls list is actually used, and thereby obtains special v features like transaction logging. This is a big ----------------- advantage of object-oriented languages like Java. | List Sorter | ----------------- | | calls v ----------------- | List ADT | -----------------02/15/1405:35:51 212JAVA INTERFACES===============Java has an "interface" keyword which refers to something quite different thanthe interfaces I defined in Lecture 8, even though the two interfaces arerelated. Henceforth, when I say "interfaces" I mean public fields, publicmethod prototypes, and the behaviors of public methods. When I say "Javainterfaces" I mean Java’s "interface" keyword.A Java interface is just like an abstract class, except for two differences.(1) In Java, a class can inherit from only one class, even if the superclass is an abstract class. However, a class can "implement" (inherit from) as many Java interfaces as you like.(2) A Java interface cannot implement any methods, nor can it include any fields except "final static" constants. It only contains method prototypes and constants. public interface Nukeable { // In Nukeable.java public void nuke(); } public interface Comparable { // In java.lang public int compareTo(Object o); } public class SList extends List implements Nukeable, Comparable { [Previous stuff here.] public void nuke() { head = null; size = 0; } public int compareTo(Object o) { [Returns a number < 0 if this < o, 0 if this.equals(o), > 0 if this > o.] } }Observe that the method prototypes in a Java interface may be declared withoutthe "abstract" keyword, because it would be redundant; a Java interface cannotcontain a method implementation.The distinction between abstract classes and Java interfaces exists because oftechnical reasons that you might begin to understand if you take CS 164(Compilers). Some languages, like C++, allow "multiple inheritance," so that asubclass can inherit from several superclasses. Java does not allow multipleinheritance in its full generality, but it offers a sort of crippled form ofmultiple inheritance: a class can "implement" multiple Java interfaces.Why does Java have this limitation? Multiple inheritance introduces a lot ofproblems in both the definition of a language and the efficient implementationof a language. For example, what should we do if a class inherits from twodifferent superclasses two different methods or fields with the same name?Multiple inheritance is responsible for some of the scariest tricks and trapsof the


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?