2.2.1 The Letter Class

Here you will learn the basics of how to create and how to work with object and classes. The example that we use for this is the Letter class mentioned before.
  1. Defining Objects and Classes
    1. What is a Class
    2. Declaring a Class
    3. Defining the Instance Variables
    4. Constructors
    5. Defining Methods
    6. Putting It All Together
  2. Using Objects and Classes
    1. Creating Letter Objects
    2. Using Instance Variables
    3. Message Passing
    4. An Example Application

Defining Objects and Classes

What is a Class

In the letter example, it is clear that the character a does not stand alone: it is just one of many letters. In OOP jargon, we say that the character a is an instance of the class of objects known as letters. A class is a template that defines how objects will look and behave, i.e., what state and behavior they will have in common. From this template, actual objects, also called instances of the class, are created or instantiated. You can instantiate many objects from one class definition: in the letter example, you can construct many characters from the letter class.

The picture of a class is the same as the above one for an object. But keep in mind that a class is not an object and vice versa. A class is a template from which actual objects are created.

The picture of the letter class may look as follows:

Declaring a Class

The simplest class, say with name Letter, can be made with
class Letter {
}
Nothing more than the class keyword, the name of the class, and curly brackets around an empty class body. In the next chapter you will see how you can relate the class that you define to other classes.

Defining the Instance Variables

In the class body you define the instance variables and methods. For the Letter class we could start with the following instance variables:
class Letter {
  String name;
  String fontname;
  int size;   
  int x, y;   
}

Constructors

You can declare a constructor method that instantiates and initializes objects for a class. An example:
Letter(String c, int s, int a, int b) { 
  // constructor to initialize letter and position
  name = c;
  fontname = "Helvetica";
  size = s;
  x = a;
  y = b;
}
You recognize a constructor method by Some side remarks:

You can define in a Java class more than one method with the same name but with different parameters. At run time, the correct choice of the method is made. In OOP jargon, Java allows method overloading.


  Letter() {   
    // constructor for default initialization
    name = "a";      
    fontname = "Helvetica";
    size = 12;
    x = 0;
    y = 0;
  }

  Letter(String c, int s, int a, int b) { 
    // constructor to initialize letter and position
    name = c;
    fontname = "Helvetica";;
    size = s;
    x = a;
    y = b;
  }

Defining Methods

The following method defines a translation over a vector (a,b):
void translate(int a, int b) {
  x = x+a;
  y = y+b;
}
Java is a strongly-typed programming language. Every variable must be declared a type and the types of the parameters of a method as well as the type of the result of a method call must be stated explicitly. void is the return type that indicates that nothing is actually returned when this method is called. The general format of defining a method looks like

accessspecifier returntype name (parameters) {body}

where the access-specifier is an optional keyword such as public to denote that a method is accessible from the outside.

Putting It All Together

Putting it all together in the file called Letter.java we have the following first implementation of the Letter class:
class Letter {
  String name;
  String fontname;
  int size;
  int x,y;

  Letter(String c, int s, int a, int b) { 
    // constructor to initialize letter and position
    name = c;
    fontname = "Helvetica";;
    size = s;
    x = a;
    y = b;
  }
 
  void translate(int a, int b) {
    x = x+a;
    y = y+b;
  }
}

Using Objects and Classes

Creating Letter Objects

A new object can be made with the help of the new operator and a constructor. To create an instance of the Letter class enter:
Letter c;          
c = new Letter("A", 12, 10, 10);
Two remarks:
Letter c = new Letter("A", 12, 10, 10);

Using Instance Variables

The state of a letter is determined by the instance variables To access or change the value of an instance variable, you use the dot notation

object.variable

In our Letter example, you can type:
/* set properties of a letter:
   name=A, fontname=Helvetica, size=24, position=(20,20). 
*/
c.name = "A";      
c.fontname = "Helvetica";
c.size = 24;
c.x = 20;
c.y = 20;
A side remark: if a comment takes more than one line, you start the opening line with /* and end the comment with */.

Message Passing

Method calls also use dot notation: the object whose method is called is on the left side of the dot; the name of the method and optionally its arguments is on the right side of the dot. The general format is as follows:

object.method(arguments);

In case of no arguments you must still place the brackets. Thus you can always distinguish between instance variables and method calls.

An example:

c.translate(30,40);
You can combine nested instance access and method calls:
c.name.length();
System.out.println(c.fontname);
You read them from the inside to the outside.

An Example Application

To convince you that the above description is correct we have collected all statements in one Java application and add some print statements to see results. In the print statements we have used string concatenation (with the + operator) to get more readable results. Do not worry about the concatenation of strings and integers that apparently occurs: the Java system takes care of automatic conversions. The Java code of the application is as follows:
class LetterApplication {

  public static void main (String[] args) {
    Letter c = new Letter();
    printInfo(c);
    c.name = "A";      
    c.fontname = "Helvetica";
    c.size = 24;
    c.x = 20;
    c.y = 20;
    printInfo(c);
    c.translate(30,40);
    printInfo(c);
  }

  static void printInfo(Letter c) {
    System.out.println(
      "letter = "    + c.name     + ", " +
      " fontname = " + c.fontname + ", " +
      " size = "     + c.size     + ", " +
      " (x,y) = ("   + c.x        + ", " +  c.y + ")"
    );
  }
}
It is stored in the file LetterApplication.java that must reside in the same directory as the file Letter.java. Then it suffices to compile the code in the file LetterApplication.java; the code in the file Letter.java that is used in the application will be compiled automatically. When you run the Java application, then you should see on your screen the following output:
letter = a,  fontname = Helvetica,  size = 12,  (x,y) = (0, 0)
letter = A,  fontname = Helvetica,  size = 24,  (x,y) = (20, 20)
letter = A,  fontname = Helvetica,  size = 24,  (x,y) = (50, 60)