Google Answers Logo
View Question
Q: java (i'm a beginner) ( Answered 5 out of 5 stars,   0 Comments )
Subject: java (i'm a beginner)
Category: Computers > Programming
Asked by: brittlehorse-ga
List Price: $10.00
Posted: 04 Nov 2002 00:42 PST
Expires: 04 Dec 2002 00:42 PST
Question ID: 97993
I need a short executive summary describing:

    a.  In general terms, the primary characteristics and the business
advantages of using an object-oriented programming.
    b.  More specific (kinda), explain object-oriented using a real
world example such as the McDonald’s

    a.  In general terms,  the difference between public and private
class variables
    b.  More specfic (yeah again)the difference between public and private
class variables using set and get methods as examples
Subject: Re: java (i'm a beginner)
Answered By: funkywizard-ga on 04 Nov 2002 04:34 PST
Rated:5 out of 5 stars
i decided to field this question since I have taken introduction to
computer science (CS103) where we covered this exact topic in detail.

According to the book "Java An Introduction to computer science and
programming second edition" by Walter Savitch (ISBN 0-13-031697-0),
"Object oriented programming is a programming methodology that views a
program as this sort of world consisting of objets that interact with
each other by means of actions."

Each object has some characteristics. It can perform actions, called
methods (which would normally be called functions in function oriented
programming). The class defines what methods an object can perform.
The second set of characteristics an object has is its variables. This
is called its data, and is also defined in the class file that the
object is created from.

As my computer science teacher always says, food and cars are the two
really great examples to use for computer programming. I'm glad you
mentioned McDonalds, since I am more familiar with food than cars.

In the Mcdonalds world, this would mean that every physical thing
would be an object that can perform actions or has data. A customer
for instance, would be able to perform the orderFood() method, the
eatFood() method, the payForFood() method, and various others. It's
data might be a set of variables including amountOfAppitite,
cashOnHand, or nameOfFavoriteFood.

This by itself is not very exciting. The main business use of this
system is in the modularity of programming in object oriented
programming. In order to use code written by someone else, all you
need to know about the code is it's interface (public methods and the
types of data they accept), which can be automagically documented by a
program called javadoc. This ability stems from three design
principles of object oriented programming:

1) Encapsulation

Encapsulation is simply a way to provide a user of a piece of code
only that information they need to use the code. Everything else is
hidden, and operates behind the scenes. As a matter of fact, when
using private variables, only the class itself even has access to
certain parts of the code, eliminating errors from user meddling. This
is different from function oriented programming, where everything is
available to be changed.

For instance, an employee at Mcdonalds does not care how the terminal
works that accepts orders and computes how much is to be paid. All the
information they require to do their job is to know which button to
push to order which item of food for the customer. There may be many
complicated things going on "beneath the hood", such as determining
what time of day it is for afternoon specials, querying a list of
items that are valid for breakfast times, or any number of things, but
all that is important to the employee is that when they push the fries
button, fries is added to the list of things the customer has ordered
and the appropriate amount is charged to the customer. All other
information would be hidden from the employee and could not be

2) Polymorphism

Polymorphism is a complex sounding name, but basically it just means
that you can name different things with the same name, and depending
on the type of data being put into the method being requested, the
correct version of the method will be selected.

For example, in the Mcdonalds world, when you order food, think of
that as an orderFood() method you can perform upon an employee (an
employee being an object). Depending on what type of food you are
ordering, an employee might have to fill up a soft drink cup, grill
some hamburgers, or simply retrieve a prepackaged item. In any case,
you still are ordering food, and thus would call the orderFood()
function. Polymorphism can recognize when you are calling a function
with a different data type (hamburgers being different from soda being
different from apple pies) and will call the correct version of the
function accordingly. This is such that you do not need a
grillBurger() function, a getSoda() function and a getPie() function.
All you need to do is remember to orderFood(), and everything else is
taken care of for you.

3) Inheritance

Inheritance is a slightly tricky topic, but basically it allows a
class to derive a set of methods and variables from a parent class,
while adding or changing those that better suit it. Inherited classes
have an "is a" relationship. For example, a Truck "is a" MotorVehicle,
so it is a good candidate for inherting. This is in contrast to a
HubCap, which a MotorVehicle has, thus a "has a" relationship. What
this allows you to do is create a base class, such as Food, that other
classes may inherit properties of. All foods can be eaten, so a
beEaten() function may exist in the Food class. All classes that
inherit from Food, will automatically have the beEaten() method
available, without having to create it from scratch. This allows for a
good amount of easy code re-use, which is good for businesses.

These three main functions of object oriented programming create an
environment that is more conducive to creating less buggy code faster,
where each task of creating a program can be easily designated to
different people, while also encouraging the most possible re-use of
existing code.

Example: A Mcdonalds employee only needs to know what buttons to push
to order food (encapsulation), the same employee only needs to know
one method to order any kind of food, be it hamburgers, soda, or
anything else (polymorphism), and so long as a customer knows how to
eatFood(), they can eat anything from a hamburger to a pie, without
having to relearn the entire eating process for each new food they
might try to eat, only learning the relevant eating-related
differences between foods.

I hope this counts as "executive summary", although I must admit it
was quite wordy.

On to private vs public variables!

In general terms, a public variable for a class can be directly
modified by any other class. What this means is that for instance, the
Customer class would have the variable amountOfAppitite. Normally, you
would only want the Customer to be able to modify this through
appropriate amounts of eatingFood(), or waitingForTimeToPass(). In the
example of a private variable, only methods such as these that exist
inside the customer class may modify the amountOfAppitite variable.

If the variable were public however, the Employee class could go ahead
and directly set your amountOfAppitite variable to some random number
at will. The Customer is expecting that their amountOfAppitite is
directly related to eatingFood() and waitingForTimeToPass(), and thus
will not suspect that this value has been changed by the Employee and
go ahead and try to be eatingFood() when they have no appitie. Since a
rogue employee set their amountOfAppitie variable to 0, at this point
the Customer would either puke or explode since their appitite would
be less than zero after eating with a zero appitite. Similarly, if
this were software, your program would likely crash.

Thus it is important to always use private variables unless you have a
specific need to use a public one.

As for get and set methods, these are basically methods that are
interfaces to the various variables in the class. When ordering food,
the Employee expects the customer to pay for it, however, it would be
undesireable if the Employee has direct public access to the
amountOfCustomersMoney. Thus you would use get and set methods.

Before giving the Customer the food, they might use a get method such
as "variablename = getCustomersAmountOfMoney()" in order to check to
make sure they have enough. Similarly, when actually taking the money
from the customer, they would call a payForFood(x) function upon the
customer, where x is the amount of money expected.

The customer then has the option to check to make sure the amount of
money being requested is valid, that they have that amount of money
available, and if no problems are found, then proceed to reduce
amountOfMoney accordingly. In the case of McDonalds, this ensures that
the cashier gets the money required for the food, while making sure
that the customer doesn't get charged more than they are expecting to

If public variables were used to allow Employee to directly reduce the
amount of money for Customer, you may get a haphazard Employee taking
more money from Customer than they have, or the incorrect amount. In
the case of McDonalds, this would likely anger the Customer, whereas
in the programming world, it would likely crash the program.

Thus private variables and get and set methods are a means of
encapsulating the program you have created thus that if your part of
the program works correctly and is designed to take into account
various potential problems as described above, no programmer can later
come along that expects to use your class and muck things up. All they
need to know is the interface for your class, and not the inner
workings of it.

I hope that you found this to be an excellent answer to your question,
and if you have any problems with this answer at all, I invite you to
request a clarification before rating the answer.
brittlehorse-ga rated this answer:5 out of 5 stars
a great help!  I really respect this researcher's comments!  THANKS

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy