by
PRO
FE S
S OR

SECTION V - Object: Instance of a class
Step 17: JAVA classes revisited
Before we continue our Mini-course, it is very convenient to discuss classes once more.
In next Step we will discuss Objects as instances of a class.
What is a class?
class Name Attributes (Variables declared)
.....
.....
.....Methods defined
.....
.....
.....
.....
.....
by
PRO
FE S
S OR

SECTION V - Object: Instance of a class
Step 18: What is an Object?
What is an Object?
An Object is an Instance of a class.
In the Object-Oriented Programming argon an Object is defined by saying that it has R.I.B.S.
This acronym stands for Relationship, Identity, Behavior, and State.
In JAVA an Object is created (a class is instantiated) by the use of the 'new' operator.
To create an Object 'quiz' of the class Exercise, we write:
quiz = new Exercise(); // The Object 'quiz' is now created as an instance of class Exercise.
Now, that we know how to instantiate a class into an Object, we can use any Instance Variables of a class,
in any static method or in any method of another class.
As we have stated in the Section III, step 9 an Instance Variable can be accessed from a static method or from
a method of another class, by referencing an Object of its class.
Let's say that we have instantiated the class Exercise and created an Object 'quiz' and
that there is an Instance variable 'questionNumber' of type 'int' in the Exercise
class.
Then in order to access that Instance variable we write:
class Exercise
{
int questionNumber;
public static void main(Stringp[] args)
{
Exercise quiz = new Exercise(); // the 'quiz' Object is created.
quiz.questionNumber= 3;
System.out.println(quiz.questionNumber);
}
}
by
PRO
FE S
S OR

SECTION V - Object: Instance of a class
Step 19: Object Cohesion and De-coupling
Object Cohesion
In Object-oriented Programming, we want to achieve a high cohesion.
As we have already discussed, an Object is an instance of a class. A class defines methods.
The methods as defined in the class can be accessed and implemented by any Object of that class.
Cohesion refers to the relationship among the methods of a given class.
For example, the Math class in JAVA contains methods that are of mathematical functionality. There exists a high cohesion in the Math class.
So, the methods in a given class, should be such that they will have some look alike functionality, or that they are somehow useful to the class where they are contained.
Object De-Coupling
In Object-oriented Programming, we want to achieve high de-coupling.
Coupling of objects refer to the relationship that exist among Objects of different classes.
That relationship is necessary for the program to work, as Objects need to communicate among
themselves.
Of course, in programming we will always have some Objects that will need to relate to other Objects; so a minimum of
coupling will always exist. But the goal is to minimize that coupling, as much as possible.
During the requirement phase of the software life cycle, a top level design
of the system, should aim toward high De-coupling and high Cohesion.