3 Inheritance

Version 4.11

Handouts:

Highlights

Inheritance means that one class is "just like that other one, except ...". At least that's Drake's initial definition (p. 67); but we should really add that inheritance means one class is a kind of another, for example, as Dog is a kind of Mammal. Class hierarchies in programming originate from classification, which is a very natural and useful way of structuring thought.

This is a very important feature of OOP, right up there with objects as bundles of data and functions.

3.1 Extending a Class

A. Example 1

This example develops classes representing a simple telephone, a cell phone, and a voice mail service, as shown in the UML class diagram (needs improvement):

UML classdiagram
UML classdiagram

The CellPhone class inherits from Telephone and adds features: the ability to set a ring tone, and voice mail. Voice mail is implemented by delegating messages (method calls) to a VoiceMail object. There is also a Tester class for demonstrating the other three.

B. Distinction Between Overriding and Overloading

Here's a method call:

a.meth(b, c)

Overriding is about a.

Overloading is about (b, c).

C. Overriding by Specialization

D. Subclasses and Interfaces

Drake: "Extending a class is similar to implementing an interface." (p. 69, last paragraph).

E. Example 2: Light and ColoredLight

F. Polymorphism and Inheritance

(p. 72) We can write either

Light bulb = new Light();

or

Light bulb = new ColoredLight();

and either way, we can safely write

bulb.setOn(false);

but how it behaves depends on what type of object it (bulb) is. Dynamic dispatch here. [Note: we could even let the actual class be determined by user input at run time. Of course, the compiler cannot predict what the user will input!

G. Chains of Inheritance

Subclasses can have subclasses; see FlashingColoredLight in Fig. 3-7, 3-6, pp. 74, 73.

Inheritance is transitive: If class A defines method foo which is inherited by subclass B, and if C is a subclass of B, then C also inherits foo.

H. Is-a and Has-a

The remaining two sections cover special topics related to inheritance: the Object class, packages, and access levels.

3.2 The Object Class

(Already covered in chapter 2)

Every object is an Object!

Object is the default superclass in Java; i.e., if we don't declare that a class "extends something" then "extends Object" is implicit.

Some methods of the Object class are generally useful, either "as is" or by overriding:

3.3 Packages and Access Levels

A. Packages

B. Access Levels

Access modifiers are Java reserved words that describe fields and methods (also classes and interfaces!) and provide information hiding. There are four access levels and three access modifier keywords:

Footnotes


  1. Revision history:
    • Version 4.1, 2012 Nov 28. Enlarged phones UML diagram; it was too small, but now it's too big.
    • Version 4, 2010 Sep 13, converted to markdown, new example
    • Version 3, stable, 2009 Sept 9, converted to rst, fixed some typos
    • Version 2-stable, 2007 Sept 5, hypertextualized.
    • Version 1-stable, 2006 Sept 6, initial version.