<< Chapter < Page Chapter >> Page >
Baldwin shows you how to extend an existing class to create a new class. The new class is the blueprint for a new type. This is the mechanism for class inheritance in Java. Inheritance provides a formal mechanism for code reuse.

Revised: Thu Mar 31 10:07:12 CDT 2016

This page is included in the following Books:

Table of contents

Preface

This module is one of a series of modules designed to teach you about the essence of Object-Oriented Programming (OOP) using Java.

Viewing tip

I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the Figuresand Listings while you are reading about them.

Figures

Listings

Preview

Extending a class

This module shows you how to extend an existing class to create a new class. The new class is the blueprint for a new type.

Inheritance and code reuse

The existing class is often called the superclass and the new class is often called the subclass . This is the mechanism for class inheritance in Java. Inheritance provides a formal mechanism for code reuse.

The subclass inherits all of the variables and all of the methods defined in the superclass.

Although not explicitly demonstrated in this module, whenever you declare a reference variable whose type is defined by a class, that variable can be usedto store references to objects instantiated from that class or instantiated from any subclass of that class. Furthermore, that reference can be used to callmethods defined in or inherited into that class with no requirement for a cast. If the method is defined in that class and overridden in the subclass, and ifthe object is actually of the subclass type, the overridden version will be executed. This is polymorphism.

Car radios with tape players

A class from a previous module (whose objects represent car radios) is extended to define a new class, whose objects represent expanded car radios that contain tape players. (Yes, at one point in history, car radios did contain tape players instead of CDs.)

Sending messages to the object

Objects of the new class know how to respond to messages for inserting, playing, and removing a tape, in addition to those messages that are appropriate for objects of the original Radio class.

Discussion and sample code

The three pillars of OOP

In an earlier module, I explained that most books on OOP will tell you that in order to understand OOP, you must understand the following three concepts:

  • Encapsulation
  • Inheritance
  • Polymorphism

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask