The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object-Oriented Programming Concepts

What Is Inheritance?

Generally speaking, objects are defined in terms of classes. You know a lot about an object by knowing its class. Even if you don't know what a penny-farthing is, if I told you it was a bicycle, you would know that it had two wheels, handle bars, and pedals.

Object-oriented systems take this a step further and allow classes to be defined in terms of other classes. For example, mountain bikes, road bikes, and tandems are all kinds of bicycles. In object-oriented terminology, mountain bikes, road bikes, and tandems are all subclasses (in the glossary) of the bicycle class. Similarly, the bicycle class is the superclass (in the glossary) of mountain bikes, road bikes, and tandems. This relationship is shown in the following figure.

Each subclass inherits (in the glossary) state (in the form of variable declarations) from the superclass. Mountain bikes, road bikes, and tandems share some states: cadence, speed, and the like. Also, each subclass inherits methods from the superclass. Mountain bikes, road bikes, and tandems share some behaviors: braking and changing pedaling speed, for example.

However, subclasses are not limited to the state and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. Tandem bicycles have two seats and two sets of handle bars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Subclasses can also override (in the glossary) inherited methods and provide specialized implementations for those methods. For example, if you had a mountain bike with an additional chain ring, you would override the "change gears" method so that the rider could shift into those lower gears.

You are not limited to just one layer of inheritance. The inheritance tree, or class hierarchy (in the glossary), can be as deep as needed. Methods and variables are inherited down through the levels. In general, the farther down in the hierarchy a class appears, the more specialized its behavior.


Note: Class hierarchies should reflect what the classes are, not how they're implemented. If we implemented a tricycle class, it might be convenient to make it a subclass of the bicycle class — after all, both tricycles and bicycles have a current speed and cadence — but because a tricycle is not a bicycle, it's unwise to publicly tie the two classes together. It could confuse users, make the tricycle class have methods (such as "change gears") that it doesn't need, and make updating or improving the tricycle class difficult.

The Object class is at the top of class hierarchy, and each class is its descendant (directly or indirectly). A variable of type Object can hold a reference to any object, such as an instance of a class or an array. Object provides behaviors that are shared by all objects running in the Java Virtual Machine. For example, all classes inherit Object's toString method, which returns a string representation of the object. The section Managing Inheritance (in the Learning the Java Language trail) covers the Object class in detail.

Inheritance offers the following benefits:


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.