{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12 - 18:08:41 CS 61B Lecture 12 Wednesday Todays reading...

Info icon This 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: 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 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.
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern