Object and Class
When creating a class imagine the objects that will be
created from that class. To design your class consider this:
- what are the things that object has
- this is known as the instance variable
- What are the actions that object can do.
- this is methods
Following is a UML diagram depicting a class design
This Movie class has two instance variables: title and
producer. Value of this variable is unique for an object. We also can say that
any two movies have different state because there are no two movies have same
title and producer.
The methods contain operation to work on the data of the
object. A pair of method like setTitle() and getTitle() are also known setter
and getter method. The purpose of this pair of method is to give or assign the
value for an instance variable and to take or retrieve current value of the
instance variable.
Class and object
A class is not an object. It is used for building and
object. A class is a blueprint for an object. It has information needed by a
compiler to build an object. Each object created from a particular class has it
own values stored in instance variable.
Imagine objects are like the order sheet at Restaurant. Each
sheet has its own blank space to be filled by customer ordering food and
drinks. When you write down the table number in the sheet means you are
creating and object. The entries you make on them are instance variable value
or state of the object.
Straight to action
To create and use an object, you will need two classes. One
class is for describing the object you want to use and another class to create
object for your class and use them. Let’s call the second class as runner class.
Inside the runner class you will put your main() method. You will create an
object in this method and test the object.
|
||||
|
||||
The dot operation
A dot operator (.) is used to access the state and behavior
of an object. For example, myPhone.color access
the color variable in the myPhone object, and myPhone.call() invokes the call() method in myPhone.
Comments
Post a Comment