Java Reference
In-Depth Information
Chapter 4
More about objects in Java
4.1 Introduction
Chapter 3 introduced the basic concepts of classes and objects in Java such as the
class definition, instantiation, and object reference. We emphasized the analogy of
classes with data types, but the class approach allows for more than just defining a
new data type. Java allows you to build upon, or inherit from, a class to create a new
child class, or subclass , with additional capabilities. In this chapter we introduce
class inheritance in Java. Inheritance involves the overriding (not overloading)
of constructors and methods, abstract classes and interfaces, polymorphism, the
Object class, and the casting of object references to sub- or superclass types.
We discuss each of these concepts in detail.
This chapter also includes additional discussion of arrays and how to use
them for vectors and matrices in mathematical operations. The chapter ends with
a couple of examples of classes for technical applications. We create an improved
complex number class and also an enhanced Histogram class.
4.2 Class inheritance
Akey feature of object-oriented programming concerns the ability of a class
to inherit from an existing class, retaining all the features of the base class but
adding new features, thus creating a subclass with increased capabilities. Here
class B inherits from class A , also known as “extending” class A (thus the Java
keyword extends ):
91
Search WWH ::




Custom Search