Java Reference
In-Depth Information
Use final
Know the Object class
I nheritance is one of the three foundation principles of object-oriented programming be-
cause it allows the creation of hierarchical classifications. Using inheritance, you can create
a general class that defines traits common to a set of related items. This class can then be
inherited by other, more specific classes, each adding those things that are unique to it.
In the language of Java, a class that is inherited is called a superclass . The class that
does the inheriting is called a subclass . Therefore, a subclass is a specialized version of a
superclass. It inherits all of the variables and methods defined by the superclass and adds
its own, unique elements.
Inheritance Basics
Java supports inheritance by allowing one class to incorporate another class into its declar-
ation. This is done by using the extends keyword. Thus, the subclass adds to (extends) the
superclass.
Let's begin with a short example that illustrates several of the key features of inheritance.
The following program creates a superclass called TwoDShape , which stores the width
and height of a two-dimensional object, and a subclass called Triangle . Notice how the
keyword extends is used to create a subclass.
Search WWH ::




Custom Search