Skip to main content

more options

Part 4. Programming style

Module 2, part 5. Abstract classes and Interfaces


The concepts of abstract classes and abstract methods solve two problems that arise in developing a system of classes and subclasses. The interface is used to define methods that a class that wants to "implement the interface" must define.


No. Topic Discussion
1. Abstract classes and methods. Read p. 4-5 of the ProgramLive CD and watch the lectures on that page. Make a class abstract so that it cannot be instantiated (instances of it cannot be created). All you can do is make subclasses of it.

Within an abstract class, make a method abstract so that it must be overridden in every subclass (that is not itself abstract).
  Reading for abstract classes and methods: Gries/Gries, Sec. 4.7, pp. 163–164. Remember that on the ProgramLive CD, p. 4-5, you can download the Java programs that are used in the lectures.
2. Interfaces. The lectures on pp. 12-1 and 12-2 of the ProgramLive CD explain the definition of an interface as well as how to implement it in other classes. Pages 12-3 and 12-4 then describe four important interfaces that are defined in the API package. You need not study this material. It is presented because the interface is an important mechanism in Java, and it is used in describing and implementing "abstract data types". It will be used heavily in CS211.

An interface gives the syntax of some methods, and any class that "implements" the interfaces must define these methods.

Interfaces provide a useful mechanism for signaling that a class will definitely implement certain methods.
  Reading for interfaces: Gries/Gries, Sec. 12.1–12.3, pp. 335–344. Sec. 12.1 of Gries/Gries describes the interface and how it is used. The other two sections give important examples of interfaces in Java.