Chapter 10. Inheritance

CHAPTER GOALS

  • To learn about inheritance

  • To understand how to inherit and override superclass methods

  • To be able to invoke superclass constructors

  • To learn about protected and package access control

  • To understand the common superclass Object and how to override its toString and equals methods

    G To use inheritance for customizing user interfaces

In this chapter, we discuss the important concept of inheritance. Specialized classes can be created that inherit behavior from more general classes. You will learn how to implement inheritance in Java, and how to make use of the Object class—the most general class in the inheritance hierarchy.

Inheritance Hierarchies

In the real world, you often categorize concepts into hierarchies. Hierarchies are frequently represented as trees, with the most general concepts at the root of the hierarchy and more specialized ones towards the branches. Figure 1 shows a typical example.

In Java it is equally common to group classes in inheritance hierarchies. The classes representing the most general concepts are near the root, more specialized classes towards the branches. For example, Figure 2 shows part of the hierarchy of Swing user-interface components in Java.

Note

Sets of classes can form complex inheritance hierarchies.

We must introduce some more terminology for expressing the relationship between the classes in an inheritance hierarchy. The more general class is called the superclass. The more specialized class that inherits from the ...

Get Big Java, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.