Download Lecture 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CompSci 230
Software Construction
Lecture Slides #4: Introduction to OOD Part 2
S1 2016
Agenda

Topics:

2
Static (class) variables & methods vs. instance variables & methods
COMPSCI 230: IOOD
Instance vs. 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.
In Java, class variables are declared as static.
Instance variables are dynamically allocated, so they



3
may have different values in each instance of an object.
When an object is instantiated, the runtime system allocates some
memory to this instance – so that it can “remember” the values it stores in
instance variables.
COMPSCI 230: IOOD
Instance & Class Methods

Instance methods operate on the object's instance variables.



Class methods are static.




4
They also have read & write access to class variables.
They can call all other methods in the class: instance methods or class
methods
Class methods cannot access instance variables.
Class methods can access class 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.)
COMPSCI 230: IOOD
Example: MixedCounter
Class variable (static)
Instance variable (not static)
public class MixedCounter {
private static int sharedCounterValue = 0;
private int counterValue = 0;
public int count() {
sharedCounterValue++;
return ++counterValue;
}
count() method is an
instance method –
increments both the
instance (individual object)
and shared counter value
public int getInstanceCount() {
return counterValue;
}
This getter returns the
count stored in the object
This getter returns the
count stored in the class
Note: When the
initialization value is
available, we can initialize
in the declaration rather
than in the constructor
public static int getSharedCount() {
return sharedCounterValue;
}
}
Question: Can we use this class as a counter without instantiating it?
5
COMPSCI 230: IOOD
Using the MixedCounter class and objects
public class MixedCounterProgram {
public static void main(String[] args) {
MixedCounter c1 = new MixedCounter();
MixedCounter c2 = new MixedCounter();
c1.count();
c2.count();
c2.count();
c1.count();
c1.count();
System.out.println("c1.getInstanceCount() returns
System.out.println("c1.getSharedCount() returns "
System.out.println("c2.getInstanceCount() returns
System.out.println("c2.getSharedCount() returns "
System.out.println("MixedCounter.getSharedCount()
MixedCounter.getSharedCount());
}
" + c1.getInstanceCount());
+ c1.getSharedCount());
" + c2.getInstanceCount());
+ c2.getSharedCount());
returns " +
}
6
COMPSCI 230: IOOD
Using the MixedCounter class and objects
public class MixedCounterProgram {
}
7
public static void main(String[] args) {
MixedCounter c1 = new MixedCounter();
Invoking a static (class) method on an object
MixedCounter c2 = new MixedCounter();
works in practice but is considered improper
c1.count();
(will produce a warning)
c2.count();
c2.count();
c1.count();
c1.count();
System.out.println("c1.getInstanceCount() returns " + c1.getInstanceCount());
System.out.println("c1.getSharedCount() returns " + c1.getSharedCount());
System.out.println("c2.getInstanceCount() returns " + c2.getInstanceCount());
System.out.println("c2.getSharedCount() returns " + c2.getSharedCount());
System.out.println("MixedCounter.getSharedCount() returns " +
MixedCounter.getSharedCount());
Console output:
}
c1.getInstanceCount() returns 3
c1.getSharedCount() returns 5
c2.getInstanceCount() returns 2
c2.getSharedCount() returns 5
Invoking a static (class) method on the class itself
MixedCounter.getSharedCount() returns 5
COMPSCI 230: IOOD
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!


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


8
When building a house, we usually have a set of plans.
Structure diagrams (documenting the architecture), e.g. class diagrams
Behaviour diagrams (documenting the functionality), e.g. use-case diagrams
COMPSCI 230: IOOD
Representing classes in UML diagrams
public class FancyMixedCounter {
private static int sharedCounterValue = 0;
private int counterValue = 0;
public FancyMixedCounter(int startValue) {
counterValue = startValue;
}
•
•
•
•
•
•
•
public FancyMixedCounter(int startValue,
int sharedStartValue) {
counterValue = startValue;
sharedCounterValue = sharedStartValue;
}
Three compartments: name, attribute, and operation
Attribute compartment takes class and instance
variables
Operation compartment takes class and instance
methods
Types of variables, parameters and return values are
specified
Constructors are preceded by <<create>>
Underlined members are static
Can add visibility (public/private) by putting a “+” or
a “–” in front of the member (not shown here)
public int count() {
sharedCounterValue++;
return ++counterValue;
}
public int getInstanceCount() {
return counterValue;
}
public static int getSharedCount() {
return sharedCounterValue;
}
}
9
COMPSCI 230: IOOD
Technical note: Turning Java classes into UML
class diagrams in ArgoUML

File -> Import sources…






10
Select .java file or directory (tree) with .java files in it
Uncheck the box “Minimise Class icons in diagrams” on the right
Hit “Open”
On the left hand panel in the main window, click on
“untitledModel”
A sub-tree will open
Click on any of the classes (small yellow icons with three
compartments) and the class diagram will appear
COMPSCI 230: IOOD
Technical note: Generating Java source code
from UML diagrams in ArgoUML





11
In the class diagram, select the classes you want to generate code
for (skip this step if you want to generate code for all classes in the
diagram)
Select Generation -> Generate selected classes … or Generation > Generate all classes… as required
A small window pops open which lets you select in which
languages you want to generate the code of each class
Select an output directory and click on “Generate”
Note: In Java, most primitive data types (int, double, boolean) are also available as classes
(Integer, Double, Boolean). When you construct UML class diagrams, ArgoUML will by
default offer you UML data types, which translate into the latter class types
COMPSCI 230: IOOD
Review

The OO approach is based on modeling the real world using interacting
objects.


The statements in a class define what its objects remember and what
they can do (the messages they can understand), that is, they define


12
Instance variables, class variables, instance methods, and class methods
A UML class diagram shows the “bare bones” of an OO system design.


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.
It need not show all classes! (A diagram should not have irrelevant information.)
Can use tools to generate UML from actual languages such as Java, and
vice versa
COMPSCI 230: IOOD
Review questions







13
If a method is declared as static, does its return value have to be stored
in a class variable?
Can an instance method set the value of a class variable in its body?
If I have several objects of the same class, how many copies of its class
variables exist in memory?
If I have several objects of the same class, how many copies of its
instance variables exist in memory?
Can a class method read the value of an instance variable?
How can we invoke a class method?
How do UML class diagrams describe the members of a class? What do
class variables look like in UML? What about instance variables? What
about constructors?
COMPSCI 230: IOOD