Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

WHAT YOU’LL LEARN IN
THIS HOUR:
.Designing superclasses
and subclasses
.Forming an inheritance
hierarchy
.Overriding methods

Java objects are ideally suited for childbearing. When you create an
object—a set of attributes and behavior—you have designed something
that’s ready to pass these qualities on to offspring.These child objects take
on a lot of the same attributes and behavior of the parent. They also can do
some things differently than the parent.


This system is called inheritance, and it’s something every superclass (par-
ent) gives to its subclasses (children). Inheritance is one of the most useful
aspects of object-oriented programming (OOP), and you learn about it dur-
ing this hour.


Another useful aspect of OOPis the capability to create an object that you
can use with different programs. Reusability makes it easier to develop
error-free, reliable programs.


The Power of Inheritance


You have used inheritance every time you worked with one of the stan-
dard Java classes such as Stringor Integer. Java classes are organized
into a pyramid-shaped hierarchy of classes in which all classes descend
from theObjectclass.


A class of objects inherits from all superclasses that are above it. To get a
working idea of how this operates, consider the JAppletclass. This class is
a superclass of all applets, browser-based Java programs that use a graphi-
cal user interface framework called Swing. The JAppletclass is a subclass
of Applet.


A partial family tree of JAppletis shown in Figure 12.1. Each of the boxes
is a class, and the lines connect a superclass to any subclasses below it.


HOUR 12


Making the Most of Existing


Objects

Free download pdf