0% found this document useful (0 votes)
31 views

Object Interaction

The document discusses object-oriented programming concepts like abstraction, modularization, classes, objects, and methods. It explains how objects can create other objects using the new operator and call methods on each other. Examples with a clock display program are used to illustrate these concepts.

Uploaded by

Esteban Rincon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Object Interaction

The document discusses object-oriented programming concepts like abstraction, modularization, classes, objects, and methods. It explains how objects can create other objects using the new operator and call methods on each other. Examples with a clock display program are used to illustrate these concepts.

Uploaded by

Esteban Rincon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Mejores son dos que uno;

porque tienen mejor paga


de su trabajo.

[Eclesiasts 4.9 La Biblia]


3.0

Part I:
Foundations of object
orientation
Object interaction
MSc. Fernando A. Rojas Morales
3.0

Main concepts to be covered

abstraction
modularization
object diagrams
object creation
method calls
debuggers

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

The OOP promise

Application Domain

Solution Domain

Application Domain Model

System Model

TrafficControl
Aircraft

UML Package

TrafficController

FlightPlan

Airport

SummaryDisplay MapDisplay
FlightPlanDatabase
TrafficControl

Object-Oriented Software Engineering Using UML, Patterns, and Java by Bernd Bruegge

Abstraction and
modularization
Abstraction is the ability focus your
attention on essentials and ignore
irrelevant details of a system.
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.
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

A digital clock

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

Modularizing the clock display

One four-digit display?

Or two two-digit
displays?

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

Classes define types


The name of a class can be used as a
variable type.
The type of a field specifies what
kind of values can be stored in the
field. If the type is a class, the field
can hold objects of that class (class
names can be used as types).

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

Implementation NumberDisplay
public class NumberDisplay
{
private int limit;
private int value;
Constructor and
methods omitted.
}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

Implementation ClockDisplay
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Constructor and
methods omitted.
}

Classes define types

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

10

Object diagram

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

11

Object Diagram
The object diagram shows the
objects and their relations in a given
moment of a running application.
It depicts the dynamic view.
The variables of type object store
references to objects (not the
objects themselves).

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

12

Class diagram

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

13

Class Diagram
The class diagram shows the classes
of an application and the relation
among them.
The class diagram gives information
about the code.
The class diagram shows the static
view.

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

14

Object diagram vs Class


diagram

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

15

Primitive types vs. object


types
SomeObject obj;

object type

int i;
32

primitive type

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

16

Primitive types
Primitive types are not Objects.
Primitive types are all predefined in
the Java language: int, boolean,
char, double, long. [Appendix B].
Primitive types dont have methods.

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

17

Quiz: What is the output?


int a;
int b;
a = 32;
b = a;
a = a + 1;
System.out.println( b );
Person a;
Person b;
a = new Person( "Everett );
b = a;
a.changeName( "Delmar );
System.out.println( b.getName() );
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

18

Primitive types vs. object


types
ObjectType a;

ObjectType b;

b = a;
int a;

int b;

32

32

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

19

The ClockDisplay source code

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

20

Source code: NumberDisplay


public NumberDisplay( int rollOverLimit )
{
limit = rollOverLimit;
value = 0;
}
public void increment()
{
value = ( value + 1 ) % limit;
}

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

21

Source code: NumberDisplay


public String getDisplayValue()
{
if( value < 10 )
{
return "0" + value;
}
else
String concatenation
{
return "" + value;
}
}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

22

The modulo operator


The 'division' operator (/), when applied to int
operands, returns the result of an integer
division.
The 'modulo' operator (%) returns the
remainder of an integer division.
E.g., generally:
17 / 5 gives result 3, remainder 2
In Java:
17 / 5 == 3
17 % 5 == 2
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

23

Quiz
What is the result of the expression
8%3

For integer n >= 0, what are all


possible results of:
n%5

Can n be negative?

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

24

Source code: NumberDisplay


public void setValue( int newValue )
{
if( ( newValue >= 0 ) &&
( newValue < limit ) )
{
value = newValue;
}
}

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

25

Logic Operators

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

26

Source code: NumberDisplay

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

27

Objects creating objects


Objects can create objects using the
new operator.
The new operation does two things:
1. It creates a new object of the
named class
2. It executes the constructor of
that class.
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

28

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();
}
}
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

29

Objects creating objects


in class ClockDisplay:
hours = new NumberDisplay( 24 );

actual parameter
in class NumberDisplay:
public NumberDisplay( int rollOverLimit );

formal parameter
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

30

ClockDisplay object diagram

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

31

Objects creating objects

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

32

Overloading
It is common for class definitions to
contain alternative versions of
constructors or methods that provide
various ways of achieving a particular
task via their distinctive sets of
parameters.
This is known as overloading a
constructor or method.
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

33

Multiple constructors

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

34

(internal) method calls


/**
* Constructor for ClockDisplay
* This constructor creates
* a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay( 24 );
minutes = new NumberDisplay( 60 );
updateDisplay();
}
Llamada desde el constructor
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

35

(internal) method calls


internal method calls
updateDisplay();

No variable name is required.


this
could be used as a reference to the
invoking object, but not used for
method calls.

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

36

(external) method calls


public void timeTick()
{
minutes.increment();
if( minutes.getValue() == 0 ) {
// it just rolled over!
hours.increment();
}
updateDisplay();
}
utiliza notacin de punto.

object . methodName ( parameter-list )


Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

37

Method calls
NB: A method call on another object
of the same type would be an
external call.
Internal means this object.
External means any other object,
regardless of its type.

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

38

null
null is a special value in Java
Object fields are initialized to null
by default.
You can test for and assign null:
private NumberDisplay hours;
if( hours != null ) { ... }
hours = null;
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

39

Summary of the clock display

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

40

Another example of object


interaction
We are concerned with understanding
how objects create other objects, and
how objects call each others methods.
The ability to read and understand
source code is one of the most
essential for a software developer.
But, we have more tools . . .
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

41

The mail system example

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

42

The debugger
Useful for gaining insights into
program behavior
whether or not there is a program
error.
Set breakpoints.
Examine variables.
Step through code.
43

The debugger (Fig. 3.5)

44

The mail system example

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

45

The this keyword

name overloading:
System.out.println( from )
Which variable will be usedthe
parameter or the field?
Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

46

Using a debugger

Setting breakpoints

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

47

Single stepping

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

48

Concepts
abstraction
modularization
classes define
types
class diagram
object diagram
object references

primitive types
object types
object creation
overloading
internal/external
method call
debugger

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Klling

49

You might also like