Java 7 for Absolute Beginners

(nextflipdebug5) #1

C H A P T E R 6


95

Object-oriented Programming


Java is an object-oriented language. That means that Java lets programmers develop software by
developing objects and specifying the relationships between the objects. The relationships are defined
within the objects, so the objects are the central focus of any software development effort in Java.

Objects


So what's an object? In technical terms, an object is an instance of a class (or the class itself if it's static or
a singleton—that is, a class of which there can be only one instance). So what exactly is an instance?
Think of it this way: A class definition is the definition for an instance, and a particular item defined by
that class is an instance. Suppose we have a class that defines documents (title, author, and so on). Then
an object that describes a particular document is an instance.
Usually, an object is a software object that represents some important part of the system.
(Unimportant parts of the system should be eliminated —most software developers are by nature or
quickly become minimalists.) Sometimes, these objects correspond to actual physical items. For
example, shipping software might have a class that represents shipping containers, and each instance of
that class corresponds to a real shipping container somewhere in the world.
Far more often, though, objects in software define abstract items—that is, things that don't really
exist. For example, the Math class defines a constant for pi. You can't touch a mathematical constant,
though it might seem real to you (take enough math classes and such things seem very real). But even a
mathematical constant is less abstract than some of the things that come up in software development.
That's because object-oriented languages such as Java let us define purely abstract objects, such as a
Shape class. In many shape-related systems, Shape itself can never be instantiated; you must instead
create an instance of some other class that extends Shape (such as a Circle class). And sometimes an
abstract class extends another abstract class, making for layers of abstraction.
Let's consider some other examples. The following are all objects that I have developed for one
system or another:


  • A class that handles button clicks

  • A class that contains information about a three-dimensional structure (such as a
    pyramid or sphere)

  • A class that contains that describes a paragraph within a document (font style,
    font size, and so on)


Java 7 for Absolute Beginners

© Jay Bryant 2012


J. Bryant,

Free download pdf