




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of object-oriented languages, focusing on encapsulation, classes, and subtyping. The author discusses the role of objects, the difference between class-based and multi-method languages, and the importance of semantics and type theory. The document also covers the creation and use of objects, subclassing, and subtyping. It provides examples using java and explains the implications for type safety.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Kim Bruce
Williams College
on leave at U. of California at Santa Cruz
2
Objects encapsulate state & methods
Self-reference
power!
4
function components?
How can semantics and type theory help?
Smalltalk, Eiffel, & Java
public class Squares {
private FramedRect outer, inner;
public Squares(Location upleft, int size,
DrawingCanvas canvas){...}
public void move(int dx, int dy) {
outer.move(dx,dy);
inner.move(dx,dy);
}
public void moveTo(int x, int y) {
this.move(x - outer.getX(),
y - outer.getY());
}
}
public class Squares {
private FramedRect outer, inner;
public Squares(Location upleft, int size,
DrawingCanvas canvas){...}
public void move(int dx, int dy) {
outer.move(dx,dy);
inner.move(dx,dy);
}
public void moveTo(int x, int y) {
this.move(x - outer.getX(),
y - outer.getY());
}
}
7
public class Squares {
private FramedRect outer, inner;
public Squares(Location upleft, int size,
DrawingCanvas canvas){...}
public void move(int dx, int dy) {
outer.move(dx,dy);
inner.move(dx,dy);
}
public void moveTo(int x, int y) {
this.move(x - outer.getX(),
y - outer.getY());
}
}
8
public class Squares {
private FramedRect outer, inner;
public Squares(Location upleft, int size,
DrawingCanvas canvas){...}
public void move(int dx, int dy) {
outer.move(dx,dy);
inner.move(dx,dy);
}
public void moveTo(int x, int y) {
this.move(x - outer.getX(),
y - outer.getY());
}
}
9
Squares fst = new Squares(corner,10,canvas);
Squares snd = new Squares(middle,40,canvas);
// objects are references
fst.moveTo(20,30);
snd = fst; // snd & fst refer to same object
snd.move(30,50);
10
First naive view of objects:
[[new Squares(...)]] =
! this.({ outer = ..., // no mention of this
inner = ... }!
{ move = fun(dx,dy). this.outer.move()...,
moveTo = fun(x,y). this.move(...) })
Defines mutually recursive methods.
Classes serve many roles:
public class OvalSquares extends Squares {
private FramedOval center;
public OvalSquares(Location upleft,
int size, DrawingCanvas canvas) {
super(upleft, size, canvas);
center = new FramedOval(...);
}
public void move(int dx, int dy) {
super.move(dx, dy); // old move
center.move(dx, dy);
}
}
19
iff
S <: S’ and T’ <: T.
S S’^ ff’ T’ T
Contravariant for parameter types.
Covariant for result types.
20
ObjType { mi : T’i } 1! i! n <: ObjType { mi : Ti } 1! i! k
iff
k! n and for all 1! i! k, T’ i
i
assuming methods not updatable at run-time!
Method parameters can vary contravariantly,
return types covariantly.
21
Java 1.4 doesn’t allow any changes to types
of methods in subclass.
C++ & Java 5 allow covariant changes to
return types.
subtype. Do you still need restrictions?
sometimes reversed.
:
void m(...) {... this.n(s) ...}
T n(S x) {...}
}
class SubExample extends Example {
T’ n(S’ x) {...}
void newMeth(...) {...}
}
What is relationship of new type of n to old if want
type safety?
method type in superclass for safety:
subclasses.
[[class(i:I,m:M)]] =
ref ]].
[[i]] " (" (this : IR’ " (IR’ # M’)). [[m]])
25
[[new Squares(...)]] =
{ outer = ref ..., inner = ref ... }
!( fm : [[I
ref ]] # [[M]]).
"( inst : [[I
ref ]]).
{ move = fun(dx,dy). inst.outer...,
moveTo = fun(x,y). inst,fm .move(...) }
Method suite can be shared between objects
of same type. 26
[[obj.p(...)]] = fm(i).p(...)
where [[obj]] = !i,fm"
In objects, methods fixed -- parameterized by suite
of instance variables, not this.
27
languages.
points.
28
[[class(i:I,m:M)]] =
ref ]].
[[i]] " " (this : IR’ " (IR’ # M’)). [[m]]
How do we type function returning this?
Defining this in Class(i : I, m : M)
[[I
ref ]]
When return this, hide instance variables
But now we have lost information.
then C m().
If D extends C, then inherited m still has
type C m().
37
In semantics:
Thus
38
Wish to type-check modularly.
assumptions that hold in all extensions!
this : ThisClass
ThisClass extends C
Can prove soundness of type system.
39
assumptions on this and ThisClass
assumption and copy inherited methods
down.
40
groups.
simultaneously in type-safe fashion.
SubjObsGrp = ClassGroup {
class MySubject {
void addObserver(MyObserver myObs) {...};
void notifyObservers: proc(MyEvent myEvt) {...}; }
class MyObserver {
void notify(MySubject mySubj, MyEvent myEvt) {...}; }
class MyEvent {…}
}
ChoiceSubjObsGrp = ClassGroup extends SubjObsGrp {
class extends MySubject {
String getItem() {...}; }
class extends MyEvent {
String getItem() {...}; }
class extends MyObserver {
void notify(MySubject mySubj, MyEvent myEvt) {
... myEvt.getItem() ...}; }
}
43
Fixed points play key role in OO languages.
44
Type-checking must take into consideration
future subclasses.
Verification should also plan for subclasses.
45
Interdependent groups of objects common.
Need to subclass simultaneously
opportunities for type-safe specialization.
46
Languages, ENTCS 82, #8, 2003.
Foundations of Object-Oriented Languages: Types
and Semantics, MIT Press, 2002.
http://www.cs.williams.edu/~kim