The Java Tutorials (Object-Oriented Programming Concepts)

Start from the beginning
                                        

belongs to some other class in your application.

What Is Inheritance?

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes,

for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines

additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop

handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example,

Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is

allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

A hierarchy of bicycle classes.

The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the

name of the class to inherit from:

class MountainBike extends Bicycle {

// new fields and methods defining a mountain bike would go here

}

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that

make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and

behavior that each superclass defines, since that code will not appear in the source file of each subclass.

What Is an Interface?

As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods

form the object's interface with the outside world; the buttons on the front of your television set, for example, are the

interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the

television on and off.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an

interface, might appear as follows:

interface Bicycle {

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);

}

To implement this interface, the name of your class would change (to ACMEBicycle, for example), and you'd use the implements

keyword in the class declaration:

class ACMEBicycle implements Bicycle {

// remainder of this class implemented as before

}

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a

You've reached the end of published parts.

⏰ Last updated: May 23, 2009 ⏰

Add this story to your Library to get notified about new parts!

The Java Tutorials (Object-Oriented Programming Concepts)Where stories live. Discover now