This article introduces you to the Java programming language. The first of two parts, it is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280).
Getting Started with Java - Object-Oriented Programming (Page 4 of 6 )
The biggest challenge for a new Java programmer is learning object-oriented programming at the same time as the Java language.
Although this might sound daunting if you are unfamiliar with this style of programming, think of it as a two-for-one discount for your brain. You will learn object-oriented programming by learning Java. There's no other way to make use of the language.
Object-oriented programming is an approach to building computer programs that mimics how objects are assembled in the physical world.
By using this style of development, you can create programs that are more reusable, reliable, and understandable.
To get to that point, you first must explore how Java embodies the principles of object-oriented programming. The following topics are covered:
Organizing programs into elements called classes
Learning how these classes are used to create objects
Defining a class by two aspects of its structure: how it should behave and what its attributes are
Connecting classes to each other in a way that one class inherits functionality from another class
Linking classes together through packages and interfaces
If you already are familiar with object-oriented programming, much of today's material will be a review for you. Even if you skim over the introductory material, you should create the sample program to get some experience developing, compiling, and running Java programs.
There are many different ways to conceptualize a computer program. One way is to think of a program as a series of instructions carried out in sequence, and this is commonly called procedural programming. Most programmers start by learning a procedural language such as Pascal or one of the many versions of BASIC.
Procedural languages mirror the way a computer carries out instructions, so the programs you write are tailored to the computer's manner of doing things. One of the first things a procedural programmer must learn is how to break down a problem into a series of simple steps.
Object-oriented programming looks at a computer program from a different angle, focusing on the task for which you are using the computer rather than the way a computer handles tasks.
In object-oriented programming, a computer program is conceptualized as a set of objects that work together to accomplish a task. Each object is a separate part of the program, interacting with the other parts in specific, highly controlled ways.
For a real-life example of object-oriented design, consider a stereo system. Most systems are built by hooking together a bunch of different objects, which are more commonly called components, such as the following:
Speaker components play midrange and high-frequency sounds.
Subwoofer components play low bass frequency sounds.
Tuner components receive radio broadcast signals.
CD player components read audio data from CDs.
These components are designed to interact with each other using standard input and output connectors. Even if you bought the speakers, subwoofer, tuner, and CD player from different companies, you can combine them to form a stereo system as long as they have standard connectors.
Object-oriented programming works under the same principle: You put together a program by combining newly created objects and existing objects in standard ways. Each object serves a specific role in the overall program.
An object is a self-contained element of a computer program that represents a related group of features and is designed to accomplish specific tasks.