SW08 - Object-Oriented Programming and Principles
Object interaction
Creating cooperating objects
2.0
A digital clock
© M. Kölling, University of Southern Denmark 1
SW08 - Object-Oriented Programming and Principles
Abstraction and
modularization
• 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, which can be built and
examined separately, and which
interact in well-defined ways.
3
Modularizing the clock
display
One four-digit display?
Or two two-digit
displays?
© M. Kölling, University of Southern Denmark 2
SW08 - Object-Oriented Programming and Principles
Implementation -
NumberDisplay
public class NumberDisplay
{
private int limit;
private int value;
Constructor and
methods omitted.
}
Implementation -ClockDisplay
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Constructor and
methods omitted.
}
© M. Kölling, University of Southern Denmark 3
SW08 - Object-Oriented Programming and Principles
Object diagram
Class diagram
© M. Kölling, University of Southern Denmark 4
SW08 - Object-Oriented Programming and Principles
Primitive types vs. object
types
SomeObject obj;
object type
int i;
32 primitive type
Primitive types vs. object
types
SomeObject a; SomeObject b;
b = a;
int a; int b;
32 32
10
© M. Kölling, University of Southern Denmark 5
SW08 - Object-Oriented Programming and Principles
Source code: NumberDisplay
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
public void increment()
{
value = (value + 1) % limit;
}
11
Source code: NumberDisplay
public String getDisplayValue()
{
if(value < 10)
return "0" + value;
else
return "" + value;
}
12
© M. Kölling, University of Southern Denmark 6
SW08 - Object-Oriented Programming and Principles
Objects creating objects
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
}
13
Method calling
public void timeTick()
{
[Link]();
if([Link]() == 0) {
// it just rolled over!
[Link]();
}
updateDisplay();
}
14
© M. Kölling, University of Southern Denmark 7
SW08 - Object-Oriented Programming and Principles
Internal method
/**
* Update the internal string that
* represents the display.
*/
private void updateDisplay()
{
displayString =
[Link]() + ":" +
[Link]();
}
15
ClockDisplay object diagram
16
© M. Kölling, University of Southern Denmark 8
SW08 - Object-Oriented Programming and Principles
Objects creating objects
in class NumberDisplay:
public NumberDisplay(int rollOverLimit);
formal parameter
in class ClockDisplay:
hours = new NumberDisplay(24);
actual parameter
17
Method calls
• internal method calls
updateDisplay();
…
private void updateDisplay()
• external method calls
[Link]();
18
© M. Kölling, University of Southern Denmark 9
SW08 - Object-Oriented Programming and Principles
Method calls (2)
object . methodName ( parameter-list )
19
Concepts
• abstraction • primitive types
• modularization • object types
• classes define • object creation
types • overloading
• class diagram • internal/external
• object diagram method call
• object references • debugger
20
© M. Kölling, University of Southern Denmark 10