Compiled Lesson 4 - Object State and Method Behavior
Compiled Lesson 4 - Object State and Method Behavior
ACTIVATING
LEARNING
PRIOR KNOWLEDGE
OBJECTIVES
In 3 to 5 sentences write what you know about
At the end of the module, the I.T. students will be methods?
able to:
__________________________________________
a. Know the how state (instance variables) __________________________________________
and behavior(methods) are related.
__________________________________________
b. Differentiate arguments from
parameters __________________________________________
c. Understand how objects behave based __________________________________________
from the state it receive. __________________________________________
TOPIC
OUTLINE
OBJECT, CLASSES,
1. Methods use object state INSTANCES
2. Method arguments and return types
3. Pass-by-value
Objects are closely related to classes. Class can
4. Getters and Setters
contain variables and methods (that is, subroutines).
5. Encapsulation Classes "describe" objects, or more exactly that the
non-static portions of classes describe objects. But
it's probably not very clear what this means. The
more usual terminology is to say that objects belong
to classes, but this might not be much clearer.
OVERVIEW (There is a real shortage of English words to
properly distinguish all the concepts involved. An
object certainly doesn't "belong" to a class in the
same way that a member variable "belongs" to a
Welcome to Java programming whether you are a class.) From the point of view of programming, it is
seasoned programmer or a complete novice, this more exact to say that classes are used to create
hand out is written to help you learn Java objects. A class is a kind of factory—or blueprint—
for constructing objects. The non-static parts of the
programming fast. Topics are carefully selected to
class specify, or describe, what variables and
give you a broad exposure to the fundamental methods the objects will contain. This is part of the
concepts of Java while not overwhelming you with explanation of how objects differ from classes:
information overload. Objects are created and destroyed as the program
runs, and there can be many objects with the same
This hand out will serve as a guide as we progress structure, if they are created using the same class.
through the class, but it can also be a valuable tool
when you are working on your own. Any class Consider a simple class whose job is to group
instruction is only as effective as the time and effort together a few static member variables. For
example, the following class could be used to store
you are willing to invest in it. I encourage you to information about the person who is using the
practice the activity given in this hand out. program:
FUNDAMENTALS
You should think of objects as floating around
OF OBJECTS independently in the computer's memory. In fact,
there is a special portion of memory called the heap
where objects live. Instead of holding an object itself,
So far, I've been talking mostly in generalities, and I a variable holds the information necessary to find the
haven't given you much of an idea about what you object in memory. This information is called a
have to put in a program if you want to work with reference or pointer to the object. In effect, a
objects. Let's look at a specific example to see how reference to an object is the address of the memory
it works. Consider this extremely simplified version location where the object is stored. When you use a
of a Student class, which could be used to store variable of object type, the computer uses the
information about students taking a course: reference in the variable to find the actual object.
This would output the name and test grades from the
object to which std refers. Similarly, std can be used
to call the getAverage() instance method in the
object by saying std.getAverage(). To print out the After the computer executes these statements, the
student's average, you could say:
1.
If the variable was declared as public, we would
expect the following output:
2.
However, as we try to access a private variable, we
get an error:
SUMMARY
Let us see if you can remember the main points
raised in this lesson. Below is a summary of these
points:
LEARNING
Getter and setter are two conventional
ACTIVITY
methods that are used for retrieving and
updating value of a variable. ... And a getter
Objective: To evaluate and check proper java is a method that reads value of a variable.
coding. Getter and setter are also known as
accessor and mutator in Java.
Task: Find out what is wrong in the following
program and explain why.
REFERENCES
Wu.C.Thomas.2004. AN introduction to
Object-oriented Programming in Java
Compiled by:
(Signed)
MARIA CLARIZA M. ESTOESTA
Professor, College of Information and Technology
Education
(Signed)
JOHANNS B. RABAGO
LORIE JELENE MILLON-PARAGAS
Professor