1. Objects First With Java
A Practical Introduction Using BlueJ
Object interaction
Creating cooperating objects
2.0
2. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
2
The next step
• so far:
– what objects are
– how they are implemented
• now:
– combine classes and objects
– let objects cooperate
– how methods call other methods
3. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
3
Project: a digital clock
hours (European style: 0..23) minutes
always the same
4. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
4
Abstraction
• Why not define the digital clock as one
single class?
– complexity substructuring (divide and
conquer)!
– effect: “abstract” from a component’s details
(not everyone needs to know everything …)
• Abstraction is the ability to ignore details
of parts to focus attention on a higher level
of a problem.
5. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
5
Modularization
• Modularization: process of dividing a
whole into well-defined parts, which can be
built and examined separately, and which
interact in well-defined ways.
• Modularization and abstraction
complement each other.
• Both concepts are crucial for object-
oriented software (components and
subcomponents being objects here).
6. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
6
Modularizing the clock
display
One four-digit display?
Or two two-digit
displays?
One or two classes?
Differences?
7. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
7
Implementation -
NumberDisplay
public class NumberDisplay
{
private int limit;
private int value;
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
methods omitted.
}
when to roll back to 0?
current value?
fields:
8. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
8
Implementation -
ClockDisplay
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Constructor and
methods omitted.
}
Class names can be used as
types!
9. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
9
Class diagrams
• class diagrams show classes and their
relationships – hence, a static view of a program
“depends on, needs”
10. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
10
Object diagrams
• object diagrams show objects and their
relationships at one moment in time – hence, a
dynamic view of a program
not really “included”,
but references only!
11. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
11
Starting the program
• Start program by creating an object of
type/class ClockDisplay.
(bluej)
• Then: automatic creation of two
NumberDisplay objects for own
purposes
12. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
12
Primitive types vs. object
types
32
object type
(by classes, given by
Java system or
by programmer)
primitive type
(predefined in Java)
Note different way of storing!
(direct value vs. reference)
SomeObject obj;
int i;
13. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
13
Primitive types vs. object
types
32
SomeObject a;
int a;
SomeObject b;
32
int b;
b = a;
two references of
the same type!
14. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
14
Source code: NumberDisplay
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit; constructor!
value = 0;
}
4 methods: getValue, setValue, getDisplayValue,
increment
public void increment()
{
value = (value + 1) % limit;
}
15. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
15
Source code: NumberDisplay
public String getDisplayValue()
{
if(value < 10)
return "0" + value;
else
return "" + value;
} why not directly return value?
public void setValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit))
value = replacementValue;
} what happens if replacement value is illegal? OK?
16. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
16
Logical operators
• operate on boolean values
• produce a new boolean value as
result
• available in Java:
– && (and)
– || (or)
– ! (not)
17. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
17
Further operators
• String concatenation
– with “+” operator
– spaces/blanks must be added explicitly
– If one operand is a string, both the
others and the result are converted to
string.
– the “+=” operator acts as for int or
double data types
• modulo: %
18. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
18
ClockDisplay object diagram
where do the three objects come from?
19. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
19
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();
}
}
20. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
20
Alternative
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
}
multiple constructors in one class possible!
Overloading
21. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
21
Automatic creation
• how and where?
– in ClockDisplay’s constructor
– automatically executed whenever a
ClockDisplay object is created
– explicitly done with new command
– new creates an object of the specified
class and executes the respective
constructor
22. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
22
Objects creating objects
hours = new NumberDisplay(24);
public NumberDisplay(int rollOverLimit);
in class NumberDisplay:
in class ClockDisplay:
formal parameter
actual parameter
23. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
23
Method calling
public void timeTick()
{
minutes.increment(); external!
if(minutes.getValue() == 0) {
// it just rolled over!
hours.increment();
}
updateDisplay(); internal!
}
24. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
24
Internal vs. external
• internal method calls
– internal means: called method belongs to calling class
(here: ClockDisplay)
updateDisplay();
private void updateDisplay()
• external method calls
– external means: called method belongs to other class
(here: NumberDisplay)
minutes.increment();
25. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
25
Calling external methods
object . methodName ( parameter-list )
26. Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling;
extensions by HJB&TN for TUM-CSE, winter 2010/2011
26
Review: concepts
• abstraction
• modularization
• classes define
types
• class diagram
• object diagram
• object references
• primitive types
• object types
• object creation
• overloading
• internal/external
method call