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

(singke) #1
ptg7068951

122 HOUR 10:Creating Your First Object


Each object has things that make it different than other objects. Pie charts
are circular, whereas bar graphs represent data as a series of rectangles. If
you break down computer programs in the same way a pie chart is broken
down, you’re engaging in OOP. In OOP, an object contains two things:
attributes and behavior. Attributesare things that describe the object and
show how it is different than other objects. Behavioris what an object does.
You create objects in Java by using a class as a template. Aclassis a master
copy of the object that determines the attributes and behavior an object
should have. The term classshould be familiar to you because Java pro-
grams are called classes. Every program you create with Java is a class that
you can use 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 Stringobject is
and behavior that controls what Stringobjects can do.
With OOP, a computer program is a group of objects that work together to
get something done. Some simple programs might seem as though they
consist of only one object: the class file. However, even those programs are
using other objects to get work done.

Objects in Action
Consider the case of the program that displays a pie chart. APieChart
objectcould consist of the following:

. Behavior to calculate the size of each pie slice
. Behavior to draw the chart
. An attribute to store the title of the chart


Bangladesh

India

Indonesia

China

United States

Pakistan
Brazil

Nigeria

Russia

Japan

FIGURE 10.1
A Java program that displays a pie
chart.

Free download pdf