Java Reference
In-Depth Information
The first object is called
john
with
John Jacobs
and
Male
as the initial values for its
name
and
gender
properties,
respectively. The second object is called
donna
with
Donna Duncan
and
Female
as the initial values for its
name
and
gender
properties, respectively.
Methods of a class represent behaviors of its objects. For example, in the real world, a person has a name and his
ability to respond when he is asked for his name is one of his behaviors. Objects of the
Person
class have abilities to
respond to three different messages:
getName
,
setName
, and
getGender
. The ability of an object to respond to a message
is implemented using methods. You can send a message, say
getName
, to a
Person
object and it will respond by returning
its name. It is the same as asking “What is your name?” and having the person respond by telling you his name.
String johnName = john.getName(); // Send getName message to john
String donnaName = donna.getName(); // Send getName message to donna
The
setName
message to the
Person
object asks him to change his current name to a new name. The following
snippet of code changes the name of the
donna
object from
Donna Duncan
to
Donna Jacobs
:
donna.setName("Donna Jacobs");
If you send the
getName
message to
donna
object at this point, it will return
Donna Jacobs
and not
Donna Duncan
.
You may notice that your
Person
objects do not have the ability to respond to a message such as -
setGender
.
The gender of
Person
object is set when the object is created and it cannot be changed afterwards. However, you
can query the gender of a
Person
object by sending
getGender
message to it. What messages an object may (or may
not) respond to is decided at design-time based on the need of the system being modeled. In the case of the
Person
objects, we decided that they would not have the ability to respond to the
setGender
message by not including a
setGender(String newGender)
method in the
Person
class.
Figure
1-3
shows the state and interface of the
Person
object called
john
.
Figure 1-3.
The state and the interface for a Person object
The object-oriented paradigm is a very powerful paradigm for modeling real-world phenomena in a computational
model. We are used to working with objects all around us in our daily life. The object-oriented paradigm is natural
and intuitive as it lets you think in terms of objects. However, it does not give you the ability to think in terms of objects
correctly. Sometimes the solution to a problem does not fall into the domain of an object-oriented paradigm. In such
cases, you need to use the paradigm that suits the problem domain the most. The object-oriented paradigm has a
learning curve. It is much more than just creating and using objects in your program. Abstraction, encapsulation,
polymorphism, and inheritance are some of the important features of the object-oriented paradigm. You must
understand and be able to use these features to take full advantage of the object-oriented paradigm. I will discuss these
features of the object-oriented paradigm in the sections to follow. In subsequent chapters, I will discuss these features
and how to implement them in a program in detail.