CIS425 Class Notes

=The Basics=

Class and its objects
A class defines the behavior and attributes of all the objects that are created from that class.

A class is implemented by writing a Java file that has the same name as the name of the class.

The source code of a class always starts with a class signature. We recognize a class signature from the keyword  class  that is followed by the name of the class.

 Class signature example : The Java file "Wombat.java" has the source code of the class Wombat. The class signature is: public class Wombat

By object behavior we mean the actions that the objects do. Object actions are implemented with methods. By object attributes we mean the properties the objects have. The object properties are implemented with instance variables.

The source code of the methods and instance variables is always placed inside the body of a class. We recognize the beginning of the class body from the opening { that follows the class signature. The end of the class body is marked by the ending }.

 Class source code example : A Wombat class that defines no attributes and no behavior for its objects has only a class signature and en empty body: public class Wombat {

}

As we said, objects are always created from a class that defines the objects' behavior and attributes. That is why objects are also called instances of a class. The most popular analogy for class and its objects is that of a blueprint of a house and all the actual houses that can be built following the same blueprint.

Once created or constructed, an object has
 * an identity, represented by a variable with some address in memory and a data type that is in fact the class of the object
 * actual data for its instance variables
 * the right to perform any of the actions implemented by the methods that are coded in the class.

 Experiment with objects 
 * Create an Wombat object in Greenfoot and place it in the world
 * Inspect the data of its instance variables.
 * Instruct the object show some of its actions.
 * What is the object's identity?

Methods
Methods implement the behavior that the objects of a class have. Method source code is placed in the body of the class.

The source code of a method is structured similarly to a class, in the sense that is has two parts:
 * method signature
 * method body

The method signature contains, at minimum, three parts:
 * return type, a keyword in the Java language, such as  int  or  void , or the name of another class, such  String .
 * name of the method, such as  foundLeaf 
 * list of parameters, enclosed within rounded parentheses and separated by commas. Each parameter has two parts: a data type and a name.

 Method signature example : Assume that wombats can perform the action of setting the direction they face in the world. The signature of the method that implements this kind of behavior is: public setDirection(int direction)

Like class source code,

Instance Variables
Instance variables implement the properties that the objects of a class have. Like method source code, instance variables source code is placed in the body of the class.