How Object-Oriented Programming Works

How Object-Oriented Programming Works

The programs you create with Java can be thought of as objects, just like physical objects that exist in the real world. Objects exist independently of other objects, interact in specific ways, and can often be combined with other objects to form something. Examples of objects in the real world include ham sandwiches, the Nixon tapes, a stereo receiver, and my father-in-law Clint. Examples of objects in a computer include a web browser, the Java compiler, an MP3 file, and a Macromedia Flash plug-in.

If you think of a computer program as a group of objects that interact with each other, you often can come up with a design for the program that's more reliable, easier to understand, and reusable in other projects.

Later in this book you will create a Java program that displays pie graphs—circles with different-colored pie slices to represent data (see Figure). A pie chart is an object that is made up of smaller objects—individual slices of different colors, a legend identifying what each slice represents, and a title.

1. A Java program that displays a pie chart.

Each object has things that make it different from other objects. Pie charts are circular, while bar graphs represent data as a series of rectangles instead of pie slices. If you break down computer programs in the same way you have broken down a pie chart, you are engaging in object-oriented programming. It's a much less complicated concept than it originally sounds.

In object-oriented programming, an object contains two things: attributes and behavior. Attributes are things that describe the object and show how it is different from other objects. Behavior is what an object does.

You create objects in Java by using a class as a template. A class is a master copy of the object that is consulted to determine which attributes and behavior an object should have. The term class should be familiar to you, because every Java program you have written thus far has been called a class. Every program you create with Java will be a class because each one can be used as a template for the creation of new objects. As an example, any Java program that uses strings is using objects created from the String class. This class contains attributes that determine what a String object is and behavior that controls what String objects can do.

With object-oriented programming, a computer program is a group of objects that work together to get something done. Some simple programs may seem as though they consist of only one object—the class file. However, even those programs are using other objects to get work done.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows