CHAPTER 50 — Inheritance

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

revised: 11/27/98, 2/14/99, 01/24/00, 07/27/02, 06/04/03, 03/04/06

CHAPTER 50 — Inheritance

Object oriented languages have a feature called inheritance. Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but has additional member variables and methods. This makes programming easier because you can build upon an existing class instead of starting out from scratch.

Inheritance (and other features of object oriented languages) is responsible for the enormous success of modern software. Programmers are able to build upon previous work and to continuously improve and upgrade existing software. For example, graphical user interface programming is done by using inheritance with the basic graphical classes that are contained in a standard library. The classes needed for a particular application are created by customizing these basic classes.

Chapter Topics:

  • Single Inheritance
  • Is-a Relationship
  • Class Hierarchies
  • Syntax of Java Inheritance
  • The super Reference
  • Method Overriding

This chapter discusses the syntax and semantics of inheritance using some simple examples. The next chapter continues the discussion using some larger examples.

QUESTION 1:

Instead of creating new classes from old classes by inheritance, couldn't you just copy the source code for the old class and modify it so that it does exactly what you want?