Skip to content

A word on Class construction

September 6, 2011

The class is the key construct in Java.  Virtually all things that you encounter, mentally, in a program could be viewed as objects, which derive from classes. The program you are writing or maintaining, if you were describing it to a non programmer, could sound like this, “this object communicates with that object which communicates with the other object”.  In other words, the program consists of lots of objects (instances of classes) communicating (messaging ) each other.

One of the hallmark *achievements of OO is the lower maintenance cost of maintaining classes.  The cost of maintaining the code or classes is less than in traditional procedural or structured programming.  This is true because there is more re-use of the code that is structured as a class.  Recall, that a class can be extended by another, thereby sharing the definitions of the parent class with its children class.  Lots of classes using the same methods or code inherited from a parent or super class equals lower maintenance.

The maintenance cost is further reduced by the class using accessor and mutator type methods to access the state of the instance variables in the object. This concept of information hiding is another best practice of class construction. Recall that each object has its own state, defined by instance variables that are created when the object is created.  The class has  fields which represent the state of the object when it is created.  Like:
public class Person{

private int age; //data field – represented as instance variable

private String name; //data field – represented as instance variable

//constructor

public Person(int age, String name){

this.age = age;

this.name = name;

}

}

This class when instantiated like:

Person person_one = new Person(25,”bob”);

Person person_two = new Person(30,”sally”);

results in two objects with the identifiers person_one and person_two, their initial state is

person_one; age = 25, name = bob

person_two;  age = 30, name = sally

The variables age and name are instance variables – instances of the state of the data for the object(s).

Hiding the instance variables from the users of the class would involve creating public accessor and mutator methods, likely defined in a parent class or better yet, an interface for the class.  Where ever it is defined, it would look similar to this:

//mutators – changing the state of instance variables

public void set_age(int arg){

this.age = arg;

}

public void set_name(String arg){

this.name = arg;

}

// accessors – accessing the state of instance variables

public int get_age(){

return this.age;

}

public String get_name(){

return this.name;

}

This hiding of the instance variable state is common practice with class construction.  Creating methods  to access (accessor) and change (mutator) the instance variables  provides a layer of abstraction for the user.  They really do not care how the data is represented in the class, they only want the state of it.

The user would see an interface for the Person class and see the accessor method get_name().  They would know how to message a instance of the Person class by using the get_name() method.  They would not know how the Person class is actually storing the name, they would just know how to get it. The information in the Person class is hidden from the other classes that want to use it.

If down the road, you decide that you are going to change the way the name is represented in your system, they you ONLY have to change it ONCE, in the class definition.  All the users who have used your class do not care if you changed how the name is stored, they only want the result of the change. No need to change code in many places where the class was instantiated.

Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: