Professional Documents
Culture Documents
Lecture 1:
Type hierarchy
2013
Outline
Type hierarchy: why & what?
Type hierarchy features
Create a type hierarchy
Subtypes with attributes
Abstract class (overview)
The meaning of subtype
Multiple implementations
Using a type hierarchy
2013
2013
Example: vehicles
P
...
Car
Bus
2013
Airplane
...
Car
Bus
2013
Airplane
...
Car
Bus
2013
Airplane
?
...
Car
2013
Bus
Airplane
2013
Benefits
Enhance ability to solve real world problems
Program modifiability:
2013
Bus
2013
Car
...
10
Road
Vehicle
Bus
2013
Car
...
Airplane
Air
Vehicle
...
Space
shuttle
IronMan
...
11
Exception
RuntimeException
(checked exceptions)
(unchecked exceptions)
2013
12
2013
13
Example
Interface PriorityVehicle represents
vehicles with priorities
Vehicle
PriorityVehicle
<<implements>>
Bus
2013
Car
14
Example: List TH
List is a sequence of elements
Two basic orders:
insertion
2013
ArrayList
LinkedList
15
List TH
Includes both classes and interfaces
<<interface>>
Collection
Queue
Abstract
Collection
List
AbstractList
ArrayList
2013
LinkedList
Vector
16
2013
17
Inheritance
Subtypes inherit attributes and operations of
the supertype and all ancestors (except
constructors):
2013
18
Setters/
getters
of other
attributes
are omitted
2013
- String name
- double width
- double height
- double length
- double weight
- int seatingCapacity
+ Vehicle(String, double, double, double, double, int)
+ getName(): String
+ setName(String)
+ calcTotalWeight(): double
+ repOK(): boolean
+ toString(): String
- validate(String, double, double, double, double, c): boolean
- validateName(String): boolean
- validateDimension(double): boolean
# validateWeight(double w): boolean
# validateSeatingCapacity(int c): boolean
FIT330 Software Engineering
19
Vehicle
- name: String
- width: double
- height: double
- length: double
- weight: double
- seatingCapacity: int
+ Vehicle(String, double, double, double, double, int)
+ getName(): String
+ setName(String)
+ calcTotalWeight(): double
+ repOK(): boolean
+ toString(): String
- validate(String, double, double, double, double, c): boolean
- validateName(String): boolean
- validateDimension(double): boolean
# validateWeight(double w): boolean
# validateSeatingCapacity(int c): boolean
Bus
Car
20
Usage
// create objects
Vehicle v1 = new Bus("b1",3.0,3.0,10.0,6000,40);
Vehicle v2 = new Car("c1",1.5,1.5,2.5,1500,4);
// use objects
System.out.println("Vehicle " + v1.getName() +
", weight: " + v1.calcTotalWeight());
Substitution principle:
super type variable can be assigned
to subtype objects
2013
21
2013
22
23
Class/interface rules
Supertype/subtype class or interface
Object is the (root) supertype of all types
Interface only has specifications
Interface can only be a subtype of another
interface
Class can be a subtype of:
2013
multiple interfaces
24
Vehicle
ch7.vehicles.Vehicle
Note:
2013
constant DomainConstraint.ZERO_PLUS
validateWeight
validateSeatingCapacity
25
Bus
ch7.vehicles.Bus
Note:
2013
validateWeight
validateSeatingCapacity
FIT330 Software Engineering
26
Car
ch7.vehicles.Car
Note:
2013
27
Bus is a PriorityVehicle
Vehicle
PriorityVehicle
<<implements>>
Bus
2013
Car
28
PriorityVehicle
ch7.vehiclesintf
.PriorityVehicle
.Bus
Note:
2013
29
Implement a TH in Java
Keyword super refers to supertype's members
Implementation can be full or partial
2013
30
Vehicle
ch7.vehicles.Vehicle
Note:
2013
31
Bus
ch7.vehicles.Bus
Note:
2013
32
Car
ch7.vehicles.Car
Note:
2013
validation methods:
33
PriorityVehicle
ch7.vehiclesintf
.PriorityVehicle
.Bus
Note:
2013
34
2013
35
Example:
Vehicle TH
Vehicle
- name: String
- width: double
- height: double
- length: double
- weight: double
- seatingCapacity: int
+ Vehicle(String, double, double, double, double, int)
...
+ repOK(): boolean
+ toString(): String
Bus
Car
- routes: int[]
- owner: String
2013
36
Bus
ch7.vehiclesextra.Bus
Note:
2013
37
Car
ch7.vehiclesextra.Car
Note:
2013
38
2013
header rule
methods rule
properties rule
39
header rule
methods rule
2013
40
When to override?
When to override a method in a subtype?
To take into account:
Example:
2013
ch7.vehiclesextra.Bus,Car
Point3.equals (later)
41
Header rule
Overriding methods must be header
compatible
Method header includes:
return type
Compatibility means:
2013
same signature
exceptions: (details
next lecture)
FIT330 Software Engineering
42
Example: Vehicle.toString
@Override
public String toString() {
return "Vehicle(" + name + ")";
}
2013
43
Methods rule
Pre-condition (@requires) is the same or
weaken:
Presuper Presub
2013
44
Example
2013
Vector
IntSet
SortedIntVector
SortedIntSet
45
Vector.add
/**
* @effects appends o to the end of this
*/
@Override
public boolean add(Object o)
2013
46
SortedIntVector.add
/**
* @effects <pre> if this is empty OR o is >= all elements
*
of this
*
super.add(o)
*
else
*
insert o at the position i in this s.t
*
xk <= o for all 0 <= k <= i-1 and
*
xj > o for all i+1 <= j < this.size
*
</pre>
*/
@Override
public boolean add(Object o)
?
47
IntSet.insert
/**
* @modifies <tt>this</tt>
* @effects
adds x to this, i.e.
*
</tt>this_post = this + {x}</tt>
*/
public void insert(int x)
2013
48
SortedIntSet.insert
/**
* @modifies <tt>this</tt>
* @effects
<pre>adds x to this, i.e.
*
this_post = this + {x},
*
such that x is greater than all elements
*
before and smaller than all elements
*
after it</pre>
*/
@Override
public void insert(int x)
?
49
Properties rule
Subtypes must preserve the supertype's
properties
Properties are explicitly stated in @overview:
rep invariant
evolution
2013
50
Overriding equals
Point2 represents points on a plane
Point3
a subtype of Point2
Two techniques:
2013
Inheritance preserving
Non-inheritence preserving
FIT330 Software Engineering
51
Inheritance preserving
class Point2 {
private int x; // x-coordinate
private int y; // y-coordinate
p can be of
public boolean equals(Object p) {
if (p instanceof Point2) {
Point2 or
// overriden behaviour
any of
return this.equals((Point2) p);
its subtypes
} else {
// default equal behaviour (last resort)
return super.equals(p);
}
}
public boolean equals(Point2 p) {
return (p != null && x == p.x && y == p.y);
}
}
2013
52
Overriding equals
class Point3 extends Point2 {
private int z; // the z coordinate
public boolean equals (Object p) {
if (p instanceof Point3) {
// overriden behaviour
return this.equals((Point3) p);
} else {
// supertype's equal behaviour
return super.equals(p);
}
}
considers the
extra z
dimension
53
Non-inheritance preserving
class Point2 {
private int x; // x-coordinate
private int y; // y-coordinate
p must be of
Point2
(subtypes not
comparable)
54
Overriding equals
class Point3 extends Point2 {
private int z; // the z coordinate
p must be of
Point3
(sub/supertypes
not comparable)
2013
55
Abstract class
A super-type that cannot be instantiated
?
56
Multiple implementations
A restricted TH: subtypes have same behaviour
Subtype is an implementation of the supertype
Using code is written using supertype except for
creating objects
Subtypes are placed in the same package
Subtypes may refer to one another
2013
57
Example: Vehicle
Vehicle
Bus
Car
2013
58
PriorityVehicle
<<implements>>
Bus
Is this TH a multi-implementation TH
2013
Car
?
59
or this?
Comparable
Vehicle
Bus
Car
Is this TH a multi-implementation TH
2013
?
60
2013
61
Example:
Vehicle vb = new Bus(...);
2013
62
Dispatching
A run-time mechanism to find the right object to
execute a method
Each object has a pointer to a dispatch vector
Dispatch vector contains references to the
object methods
Method invocation is dispatched to the target
implementation
2013
63
Dispatching example
Stack
Heap
equals
o
r
<,abc>
Code for
String equals
<,abc>
Dispatch
vector
String t = "ab";
Object o = t + "c";
String r = "abc";
boolean b = o.equals(r);
2013
64
Summary
THs make program structure easier to
understand
THs enables shared specification for the
related types
THs supports extensibility
Subtypes obey the substitution principle
2013
65
Questions?
2013
66