<< Chapter < Page Chapter >> Page >

Good OOP practice

Good object-oriented programming practice says that most of the instance variables encapsulated in an object should be declared private. If thereis a need to make the contents of those variables available outside the object, that should be accomplished by defining public accessor methods. (Accessor methods are often referred to as getter methods because the name of the accessor method often includes the word "get".)

Setter methods

If there is a need for code outside the object to store information in the object's private instance variables, this should be accomplished by writingpublic setter methods. Code in the setter methods can filter incoming data to make certain that the state of the object doesn't becomecorrupt as a result of outside influences.

Pass and return by value

Everything in Java is passed and returned by value , not by reference.

Each of the accessor methods shown in Listing 4 returns a copy of the reference belonging to either a Turtle object or a World object.

Pass to the println method

As you saw earlier, each of the three references is passed to the println method in Listing 1 causing information about the objects to be displayed on the command-linescreen.

The toString method

Although it isn't obvious in Listing 1 , the code in the println method calls a method named toString on the incoming object reference and displays the string value returned by that method. I discuss the toString method in some detail here and here .

An overridden method

The toString method is overridden (not overloaded) in the World and Turtle classes so as to return a string value describing the object.

The Ericson javadocs

Normally, the javadocs would tell you what information is contained in that string, but that is not the case in Ericson's javadocs. You would have toget into her source code, (which is readily available), to get that information. However, you can see the information that is contained in thestring values for the two different types of objects in the last three lines of text in Figure 2 .

The beginning of the run method

This is where thing start to get interesting. Listing 5 shows the beginning of the public method named run .

Listing 5. The beginning of the run method.
public void run(){ //Replace the default all-white picture with another// picture. mars.setPicture(new Picture("Prob01.jpg"));

Recall that the code in the main method in Listing 1 calls the run method on the object immediately after it is instantiated.

A turtle on a white background

I told you earlier that if you were to eliminate the call to the run method, you would see a turtle at the center of the world with a white background.

The background is a Picture object

The background of a World object consists of an object of Ericson's Picture class. (A Picture object is encapsulated in the World object.)

By default, the Picture object encapsulated in a World object is all white and is exactly the right size and shape to completely fill the area inside the world's border (see Figure 1 ).

Can be replaced

As you will see shortly, we can replace the default Picture object with a new Picture object of our own choosing.

Questions & Answers

distinguish between anatomy and physiology
Amina Reply
Anatomy is the study of internal structure of an organism while physiology is the study of the function/relationship of the body organs working together as a system in an organism.
adeyeye
distinguish between anatomy and physiology
Erny Reply
regional anatomy is the study of the body regionally
Ismail Reply
what is the meaning of regional anatomy
Aminat Reply
epithelial tissue: it covers the Hollow organs and body cavities
Esomchi Reply
in short way what those epithelial tissue mean
Zainab Reply
in short way what those epithelial tissue mean
Chizoba
What is the function of the skeleton
Lilias Reply
movement
Ogar
Locomotion
Ojo
support
Aishat
and body shape/form
Aishat
what is homeostasis?
Samuel Reply
what's physiology
AminchiSunday Reply
what is physiology
AminchiSunday
physically is the study of the function of the body
Najaatu
that is what I want ask
YAU
u are wright
YAU
pls what are the main treatment of hiccups
YAU
physiology is the study of the function of the body
Najaatu
hiccups happen when something irritates the nerves that course your diaphragm to contract
Najaatu
how did hypothalamus manege to control all activities of the various hormones
malual
what is protein
Abdulsalam
how can I treat pain a patient feels after eating meals
Namuli Reply
how do I treat a three year old baby of skin infection?
Okocha Reply
It depends on the type of infection. Bacterial, fungal, parasitic or viral?
schler
if you can share the sign ad symptoms of the skin infection then u geh the treatment cox they're different sign ad symptoms of skin infection with different treatment
Sa
the sign and symptoms of maleria
Abdulsalam
prostaglandin and fever
Maha Reply
yes
rayyanu
welcome sir
rayyanu
prostaglandin E2 is the final mediator.
Lemlem
prostaglandin E2 is the final mediator of fever.
Lemlem
yes
Agabi
good evening
Jediel
tissue.
Akoi
explain
Chizoba
Hi
Anya
,good evening
Anya
Discuss the differences between taste and flavor, including how other sensory inputs contribute to our  perception of flavor.
John Reply
taste refers to your understanding of the flavor . while flavor one The other hand is refers to sort of just a blend things.
Faith
While taste primarily relies on our taste buds, flavor involves a complex interplay between taste and aroma
Kamara
which drugs can we use for ulcers
Ummi Reply
omeprazole
Kamara
what
Renee
what is this
Renee
is a drug
Kamara
of anti-ulcer
Kamara
Omeprazole Cimetidine / Tagament For the complicated once ulcer - kit
Patrick
what is the function of lymphatic system
Nency Reply
Not really sure
Eli
to drain extracellular fluid all over the body.
asegid
The lymphatic system plays several crucial roles in the human body, functioning as a key component of the immune system and contributing to the maintenance of fluid balance. Its main functions include: 1. Immune Response: The lymphatic system produces and transports lymphocytes, which are a type of
asegid
to transport fluids fats proteins and lymphocytes to the blood stream as lymph
Adama
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask