Class Relationship: Object Oriented Programming
Class Relationship: Object Oriented Programming
CLASS RELATIONSHIP
Array in Java
1
9/30/2019
Array
• the second kind of reference types in Java
• an ordered collection, or numbered list, of values.
• The values can be primitive values, objects, or even other
arrays, but all of the values in an array must be of the same
type.
Declaring Array
• byte b; // byte is a primitive type
• byte[] arrayOfBytes; // byte[] is an array type: array of byte
• byte[][] arrayOfArrayOfBytes; // byte[][] is another type: array of byte[]
• Point[] points; // Point[] is an array of Point objects
2
9/30/2019
Array
Dog[] pets;
pets = new Dog[7];
pets[0] = new Dog()
pets[1] = new Dog()
3
9/30/2019
Creating Array
• Use the new keyword, just as you do to create an object,
however arrays don't need to be initialized like objects do
• Must specify the length, once created the array size cannot be
changed
Array Literals
• The null literal used to represent the absence of an object can
also be used to represent the absence of an array.
– char[] password = null;
4
9/30/2019
Multidimensional Array
• int[][] products;
Multidimensional Array
• The new keyword performs this additional initialization
automatically for you. It works with arrays with more than
two dimensions as well:
5
9/30/2019
Multidimensional Array
• When using new with multidimensional arrays, you do not
have to specify a size for all dimensions of the array, only the
leftmost dimension or dimensions.
Multidimensional Array
• The first line creates a single-dimensional array, where each
element of the array can hold a float[][].
• The second line creates a two-dimensional array, where each
element of the array is a float[].
• If you specify a size for only some of the dimensions of an
array, however, those dimensions must be the leftmost ones.
The following lines are not legal:
6
9/30/2019
CSG2H3
Object Oriented Programming
Class Diagram
7
9/30/2019
Classes
A class is a description of a set of
ClassName objects that share the same attributes,
operations, relationships, and semantics.
attributes
Graphically, a class is rendered as a
rectangle, usually including its name,
operations attributes, and operations in separate,
designated compartments.
Class
Name Window
size: Size
Attributes visibility: boolean
display()
Operations hide()
8
9/30/2019
Class Names
operations
Class Attributes
Person
9
9/30/2019
Class Attributes
Attributes are usually listed in the form:
/ age : Date
Class Attributes
Person
10
9/30/2019
Class Operations
Person
name : String
address : String
birthdate : Date
age : integer
idNumber : integer
Class Operations
PhoneBook
You can specify an operation by stating its signature: listing the name,
type, and default value of all parameters, and, in the case of
functions, a return type.
11
9/30/2019
Example
public class Person{
private String name;
private String address;
private Date birthdate;
private int idNumber;
Depicting Classes
When drawing a class, you needn’t show attributes and operation in
every diagram.
Person Person
Person
name : String
birthdate : Date
Person ssn : Id
12
9/30/2019
Class Relationship
Class Association
13
9/30/2019
Association Relationships
If two classes in a model need to communicate with each other,
there must be link between them.
Student Instructor
Example
public class Student{
private String name;
private String assignment;
14
9/30/2019
Example
public class Instructor{
private String name;
Association Relationships
We can indicate the multiplicity of an association by adding
multiplicity adornments to the line denoting the association.
Student Instructor
1..*
15
9/30/2019
Association Relationships
Student Instructor
1..*
Association Relationships
We can also indicate the behavior of an object in an association
(i.e., the role of an object) using rolenames.
16
9/30/2019
Association Relationships
We can also name the association.
membership
Student Team
1..* 1..*
Association Relationships
We can specify dual associations.
member of
1..* 1..*
Student Team
1 president of 1..*
17
9/30/2019
Association Relationships
We can constrain the association relationship by defining the
navigability of the association. Here, a Router object requests
services from a DNS object by sending messages to (invoking the
operations of) the server. The direction of the association indicates
that the server has no knowledge of the Router.
Router DomainNameServer
Association Relationships
Associations can also be objects themselves, called link classes or
an association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
18
9/30/2019
Association Relationships
next
LinkedListNode
previous
Aggregation Relationships
We can model objects that contain other objects by way of
special associations called aggregations and compositions.
Engine
Car
Transmission
19
9/30/2019
Example
public class Engine { public class Transmission {
private String name; private String type;
private int horsePower;
public String getType() {
public Engine(String name){ return type;
this.name = name; }
}
public void setType(String type) {
public int getHorsePower() { this.type = type;
return horsePower; }
} }
Example
public class Car {
private String name;
private Engine engine;
private Transmission transmission;
20
9/30/2019
Example
public class Driver {
c.addEngine(v1000);
c.addTransmission(auto);
}
}
Composition Relationships
Composition is a stronger variant of the "owns a" or association
relationship
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a whole).
Compositions are denoted by a filled-diamond adornment on the
association.
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 .. *
21
9/30/2019
Example
public class Scrollbar {
public String type;
Example
public class Menu {
private String title;
private String type;
22
9/30/2019
Example
public class Window {
private Scrollbar scBar;
private Titlebar tlBar;
private Menu[] menu;
Interfaces
23
9/30/2019
Example
Specify the appropriate relation to each case:
• A person and a car that he wants to buy
• A car in a parking lot
• A mall and its parking lot
• Wheels in a car
• A department and a company
• A department and an employee
• A canteen and a department
• A pond and fishes
Exercise
24