The BASE for JAVA LEARNING

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?
  • A class is a template, a blueprint. It's considered a non-primitive type data.
  • A class holds the declaration of variables of many different data types, and different classes.
  • The same way as a variable is of a particular data type, an Object is of a particular class.
  • A class contains attributes(variables) as well as methods.
A class template:
class Name
Attributes (Variables declared)
.....
.....
.....
Methods defined
.....
.....
.....
.....
.....

Click here to go to the JAVA Mini-Course page

The BASE for JAVA LEARNING

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: Exercise quiz = new Exercise(); // Notice the set of parenthesis after Exercise. That also can be written in 2 lines: Exercise quiz;               // quiz is declared of the Exercise class
quiz = new Exercise(); // The Object 'quiz' is now created as an instance of class Exercise.
Notice that in the right hand of the equal sign, the class name is followed by a set of parenthesis. That represents a Constructor and we will discuss Constructors in a later Section of this Mini-Course. We will also see more about the handling of Objects.



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: quiz.questionNumber In the Example below, an Object 'quiz' is created as an instance of the Exercise class. The Instance variable 'questionNumber' is accessed within the main method which is a class method (static).

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);
      }
}

Click here to go to the JAVA Mini-Course page

The BASE for JAVA LEARNING

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.

A higher cohesion is accomplished, if methods in a given class are of some common functional characteristics.

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.
High cohesion is desirable, and by having a high cohesion in our code we will probably increase the de-coupling among objects. As you will see next, high de-coupling is very much desirable in Object-oriented Programming.


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.

However, it is convenient to diminish this coupling among Objects. De-coupling is the opposite of coupling, and it's a break in the relationship. When we de-couple two Objects, that means that both objects will not be related, and changes to one will not affect the other. If two Objects are coupled tightly, changes on one Object will affect the other Object.

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.

Click here to go to the JAVA Mini-Course page