Chapter 6

Extending Classes and Inheritance

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to reuse classes by defining a new class based on an existing class
  • What polymorphism is and how to define your classes to take advantage of it
  • What an abstract method is
  • What an abstract class is
  • What an interface is and how you can define your own interfaces
  • How to use interfaces in your classes
  • How interfaces can help you implement polymorphic classes

A very important part of object-oriented programming enables you to create a new class based on a class that has already been defined. The class that you use as the base for your new class can be one that you have defined, a standard class in Java, or a class defined by someone else — perhaps from a package supporting a specialized application area.

This chapter focuses on how you can reuse existing classes by creating new classes based on the ones you have and explores the ramifications of using this facility, and the additional capabilities it provides. You also delve into an important related topic — interfaces — and how you can use them.

USING EXISTING CLASSES

Let’s start by understanding the jargon. Defining a new class based on an existing class is called derivation. The new class, or derived class, is referred to as a direct subclass of the class from which it is derived. The original class is called a base class because it forms the base for the definition of the derived class. The original class is also referred to as a superclass of ...

Get Ivor Horton's Beginning Java®, Java 7 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.