Supplementary notes for CS 101 Lecture 27

Definitions

Inheritance
A mechanism for re-using code. A class inherits instance variables and methods from its superclass.

Abstract class
A class that can not be instantiated. Sole purpose is to define features and behavior common to subclasses. Ex: Component

Polymorphism
The ability of methods in different classes to share the same name. Ex: toString() is a polymorphic message

Shape class

Suppose we wanted to define an abstract Shape class with concrete subclasses Circle and Rect.

Common elements in Shapes:

Implementation of the Shape class

public abstract class Shape extends Object {
 ...
}

public class Circle extends Shape {
 ...
}

public class Rect extends Shape {
 ...
}

*** See code example for Lecture 27: Shape.java ***

Notes:

abstract methods: no code. Purpose is to require subclasses to provide functionality. Example: Shape.area() and Shape.stretchBy()

final methods: methods that can't be redefined in subclasses. Example: Shape.getXPos(), Shape.getYPos()

final classes: can not have subclasses. Example: Java classes.

Circle class

*** See code example for Lecture 27: Circle.java ***

*** See code example for Lecture 27: Rect.java ***

Notes: