Lecture2 - Software-Construction-BSSE5
Lecture2 - Software-Construction-BSSE5
Introduction to Object-
Orientation
Message Passing
In a method call, a message is passed to a
receiver object.
The receiver’s response to the message is
determined by its class.
Ball b = new Ball(10, 20, Color.Red);
b.move(50, 100);
b: Ball
xPos =10
= 10
60
yPos = 20
message
receiver yPos
Color=
= Red
arguments 20120
Color = Red
public class Ball {
...
public void move(int deltaX, int deltaY) {
xPos += deltaX;
yPos += deltaY;
}
}
2
Instance & Class Variables
Class variables are statically allocated, so they
are shared by an entire Class of objects.
The runtime system allocates class variables once
per class, regardless of the number of instances
created of that class.
Static storage is allocated when the class is loaded.
All instances share the same copy of the class
variables. : Class
name =
Instance variables are dynamically allocated, so “Ball”
they size = 10
may have different values in each instance of an
object. b1: Ball b2: Ball
xPos=10 xPos =10
When an object is instantiated, the runtime system
yPos = 20 yPos = 10
allocates some memory to this instance – so that it
can “remember” the values it stores in instance Color = Red Color =
Blue
variables.
3
Instance & Class Methods
Instance methods operate on public class Class1 {
private int x;
this object's instance variables. public int increment() {
++x;
They also have read & write access }
}
to class variables.
Class methods are static.
Class methods cannot access
instance variables.
Class methods are handled by the
“class object” – they can be called
even if there are no instances of
this class.
(Example on the next slide.)
4
Class1App
public class Class1App {
public static void main( String[] args ) {
Class1 x = new Class1();
System.out.println(
"Without initialisation, ++x = "
+ x.increment()
);
System.out.println(
"After another incrementation, ++x = "
+ x.increment()
);
}
} 5
BallApp
import java.awt.*; public void paint(Graphics g) {
import java.awt.event.*; b.paint( g );
}
public class BallApp extends Frame{
Ball b = new Ball( 20, 30, Color.blue ); public static void main(
String[] args
public BallApp() {
) {
addWindowListener(
new BallApp();
new WindowAdapter() {
}
public void windowClosing(
WindowEvent e }
) {
System.exit( 0 );
}
}
);
setSize( 300, 200 );
setVisible( true );
}
6
public class SharedCounter {
private static int count=0;
private int value;
public SharedCounter(int param) {
this.value = param;
++count;
} ::: Class
Class
Class
public int getValue() {
name
name
name===
return value;
}
“SharedCounter”
“SharedCounter”
“SharedCounter”
public static int getCount() { count
count=
count ==00011223
return count;
}
public String toString() {
c1:
return "value=" + value + " count=" + count; SharedCounter
}
value = 10
}
c2:
public static void main(String[] args) { SharedCounter
SharedCounter c1 = new SharedCounter(10);
value = 100
SharedCounter c2 = new SharedCounter(100);
SharedCounter c3 = new SharedCounter(200); c3:
System.out.println(c1 + " " + c2 + " " + c3); SharedCounter
}
value = 200
7
UML
Unified Modeling Language (UML)
When creating complex OO systems, where do we start?
When building complex systems, it might be worthwhile to plan
things out before you start coding!
When building a house, we usually have a set of plans.
UML is a language which allows us to graphically model an OO
system in a standardised format.
This helps us (and others!) understand the system.
There are many different UML diagrams, allowing us to model
designs from many different viewpoints. Roughly, there are
Structure diagrams (documenting the architecture), e.g. class
diagrams
Behaviour diagrams (documenting the functionality), e.g. use-case
diagrams
8
Object Diagrams in UML
An object diagram is a graphic representation of an instance
model, showing the state of a system after some objects have
been instantiated, and after some variables of these objects have
been updated.
Please focus on the basics.
Understand the distinction between static variables and instance
variables.
Develop a working understanding of instantiation
Learn how to draw UML-standard class diagrams.
Learn more about object diagrams. “Modelling
instances of classifiers using UML object diagrams”, online Help resource
for the IBM Rational Software Modeler, available 4 March 2014.
Eclipse plugin for UML: http://www.objectaid.com/update/current
Offline Tools: Rational Rose, StarUML
Online Tools: Gliffy, Draw.io
For
9 ERD Design, MySQL Workbench
Review
The OO approach is based on modeling the real world using
interacting objects.
OO design is a process of determining what the stakeholders require,
designing a set of classes with objects which will meet these
requirements, implementing, and delivering.
The statements in a class define what its objects remember and
what they can do (the messages they can understand), that is,
they define
Instance variables, class variables, instance methods, and class
methods
The hardest concept in this set of lecture slides: instantiation.
Very important!
A UML class diagram shows the “bare bones” of an OO system
design.
It need not show all classes! (A diagram should not have irrelevant
information.)
10