Object interaction

Alberto Ferrari
Ingegneria dell'Informazione, UniPR

Concepts

  • abstraction
  • modularization
  • object creation
  • object diagrams
  • method calls

Abstraction & Modularisation

  • Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem
  • Modularisation is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways
  • Divide and conquer (divide et impera): divide the problem into sub-problems, then again into sub-sub-problems, and so on, until the individual problems are small enough to be easy to deal with

Example: a digital clock

  • The project we use is a display for a digital clock
  • The display shows hours and minutes, separated by a colon
  • Modularising the clock display
    • one four-digits display? ("11:03")
    • or two two-digits diaplay? ("11") ("03")
    • and a bit of glue? (":")

Implementation NumberDisplay

public class NumberDisplay
{
    private int limit;
    private int value;

    ... constructor omitted
    ... methods omitted
}

Implementation ClockDisplay

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    ... constructor omitted
    ... methods omitted
}

Object diagram

  • The object diagram shows the objects and their relationships at one moment in time during the execution of an application
  • It gives information about objects at runtime and presents the dynamic view of a program

Object diagram: example

Object diagram An object of class ClockDisplay contains two references (each is a field) to objects of class NumberDisplay

Class diagram

  • The class diagram shows the classes of an application and the relationships between them.
  • It gives information about the source code and presents the static view of a program.

Class diagram: example

Class diagram

Class ClockDisplay contains a reference (either through a field, method parameter or method local variable) to an object that is a NumberDisplay

Classes define types

  • A class name can be used as the type for a variable
  • Variables that have a class as their type can store objects of that class
  • Object references. Variables of object types store references to objects.
  • Class defines the set of values (fields) and the set of operations (methods) for the new type defined
  • Primitive types (int, boolean, char, double, long ...) are the non-object types.
  • Primitive types have no methods.

Types

Primitive types vs. Object types

Primitive types vs. Object types

Quiz

int a, b;
a = 42;
b = a;
a = a + 1;                 // has b changed?
System.out.println(b);

Person a, b;
a = new Person("Jack");
b = a;
a.changeName("John");    // has b changed?
System.out.println(b.getName());

Source code: NumberDisplay

public NumberDisplay(int rollOverLimit) {
    limit = rollOverLimit;
    value = 0;
}

public void increment() {
    value = (value + 1) % limit;
}

public String getDisplayValue() {
    if (value < 10) {
        return "0" + value;
    }
    else {
        return "" + value;
    }
}

Object creation

Objects can create other objects, using the new operator

Objects references Objects

public class ClockDisplay {
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;

    public ClockDisplay() {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        updateDisplay();
    }

    private void updateDisplay() {
        displayString = hours.getDisplayValue() + ":" +
          minutes.getDisplayValue();
    }

... }

Method calling

  • External method call with dot notation
    • Calling a method on another object (only public methods)
objectName.methodName(parameterList);
  • Internal method call
    • Calling a method on our own object (public and private methods)
methodName(argument);
  • or
this.methodName(argument);

Method calling (Example from ClockDisplay)

/**
 * Advance the time by one minute.
*/
public void timeTick() {
    minutes.increment();
    if(minutes.getValue() == 0) { // it just rolled over!
        hours.increment();
    }
    updateDisplay();
}

null object

  • null is a special Object in Java
  • All Object variables (of any class) are initially null
  • Variables can be tested to see if they are null
  • Example:
private NumberDisplay hours;

public void makeHoursVisible() {  
  if (hours == null) {      
    ...  nothing to show  
  } else {
    ...  display it  
  }
}

Overloading

  • A class may contain more than one constructor, or more than one method of the same name, as long as each has a distinctive set of parameter types

Overloading: example

/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at 00:00.
 */
public ClockDisplay() {
    hours = new NumberDisplay(24);
    minutes = new NumberDisplay(60);
    updateDisplay();
}
/**
 * Constructor for ClockDisplay objects. This constructor
 * creates a new clock set at the time specified by the parameters.
 */
public ClockDisplay(int hour, int minute) {
    hours = new NumberDisplay(24);
    minutes = new NumberDisplay(60);
    setTime(hour, minute);
}

Exercise

  • Change the clock from a 24-hour clock to a 12-hour clock. Be careful: This is not as easy as it might at first seem. In a 12-hour clock, the hours after midnight and after noon are not shown as 00:30, but as 12:30. Thus, the minute display shows values from 0 to 59, while the hour display shows values from 1 to 12!
  • There are (at least) two ways in which you can make a 12-hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can simply leave the clock to work internally as a 24-hour clock but change the display string of the clock display to show 4:23 or 4.23pm when the internal value is 16:23.
  • Implement both versions.
    • Which option is easier?
    • Which is better?
    • Why?

Concepts Summary (1)

  • abstraction is the ability to ignore details of parts, to focus attention on a higher level of a problem.
  • modularization is the process of dividing a whole into well-defined parts that can be built and examined separately and that interact in well-defined ways.
  • classes define types: a class name can be used as the type for a variable. Variables that have a class as their type can store objects of that class.
  • class diagram shows the classes of an application and the relationships between them. It gives information about the source code and presents the static view of a program.
  • object diagram shows the objects and their relationships at one moment in time during the execution of an application. It gives information about objects at runtime and presents the dynamic view of a program.

Concepts Summary (2)

  • object references: variables of object types store references to objects.
  • primitive types in Java are the non-object types. Types such as int, boolean, char, double, and long are the most common primitive types. Primitive types have no methods.
  • object creation: objects can create other objects, using the new operator.
  • overloading: a class may contain more than one constructor, or more than one method of the same name, as long as each has a distinctive set of parameter types.
  • internal method call: methods can call other methods of the same class as part of their implementation.
  • external method call: methods can call methods of other objects using dot notation.

Alberto Ferrari
Ingegneria dell'Informazione, UniPR
www.ce.unipr.it/~aferrari/