Understanding Object-Oriented Languages: Encapsulation, Classes, and Subtyping, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-mvl
koofers-user-mvl 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Kim Bruce
Williams College
on leave at U. of California at Santa Cruz
Fixing the Meaning of
Object-Oriented Languages
2
Objects encapsulate state & methods
Subtyping
Inheritance
Self-reference
-Provides dynamic method invocation with more
power!
What makes O-O Languages
Special?
Luca Cardelli:
"If there is no 'self' in a
language then it is not
object-oriented."
4
Are objects more than records with
function components?
What provides real power?
How can semantics and type theory help?
Focus on class-based O-O languages like
Smalltalk, Eiffel, & Java
-Multi-method languages are quite different
What’s the Big Deal?
5
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());
}
}
Defining a Class
6
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());
}
}
Instance Variables
pf3
pf4
pf5
pf8

Partial preview of the text

Download Understanding Object-Oriented Languages: Encapsulation, Classes, and Subtyping and more Study notes Computer Science in PDF only on Docsity!

Kim Bruce

Williams College

on leave at U. of California at Santa Cruz

Fixing the Meaning of

Object-Oriented Languages

2

Objects encapsulate state & methods

• Subtyping

• Inheritance

Self-reference

- Provides dynamic method invocation with more

power!

What makes O-O Languages

Special?

Luca Cardelli:

"If there is no 'self' in a

language then it is not

object-oriented."

4

• Are objects more than records with

function components?

• What provides real power?

How can semantics and type theory help?

• Focus on class-based O-O languages like

Smalltalk, Eiffel, & Java

- Multi-method languages are quite different

What’s the Big Deal?

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());

}

}

Defining a Class

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());

}

}

Instance Variables

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());

}

}

Constructor

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());

}

}

Methods

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);

Creating & Using Objects

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.

Objects Are Fixed Points

Classes serve many roles:

  • Types
  • Generators of new objects
  • Extensible to form new generators

Classes Are Generators

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);

}

}

Subclass

What happens to moveTo?

19

S’! T’ <: S! T

iff

S <: S’ and T’ <: T.

Subtyping Function Types

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

<: T

i

assuming methods not updatable at run-time!

Method parameters can vary contravariantly,

return types covariantly.

Subtyping Object Types

21

Java 1.4 doesn’t allow any changes to types

of methods in subclass.

C++ & Java 5 allow covariant changes to

return types.

  • Suppose you don’t care if subclass gives a

subtype. Do you still need restrictions?

  • In Smalltalk, subclass and subtype hierarchies

sometimes reversed.

Restriction on Subclass Changes class Example {

:

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 subclass must be subtype of

method type in superclass for safety:

  • Covariant change allowed in return type
  • Contravariant change in parameter type

Restriction on Subclass Changes

  • Methods must be meaningful in all possible

subclasses.

[[class(i:I,m:M)]] =

M’ <: [[M]]. IR’ <: [[I

ref ]].

[[i]] " (" (this : IR’ " (IR’ # M’)). [[m]])

initial values methods

of instance variables

Semantics of Classes?

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(...) }

Semantics of Objects

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.

Sending Messages

27

  • Fixed points^ are key to understanding O-O

languages.

  • Classes are^ extensible^ generators^ of fixed

points.

  • Subtyping^ explains restrictions on subclasses
    • Even though subtyping distinct concept.

Summary

28

What is Type of "this"?

[[class(i:I,m:M)]] =

M’ <: [[M]]. IR’ <: [[I

ref ]].

[[i]] " " (this : IR’ " (IR’ # M’)). [[m]]

How do we type function returning this?

Finding a Type for "this"

Defining this in Class(i : I, m : M)

  • this : IR’ " (IR’ # M’) where M’ <: [[M]], IR’ <:

[[I

ref ]]

When return this, hide instance variables

  • this : X. X " (X # M’) = Obj(M')
  • but notice Obj(M') <: Obj(M), thus this : Obj(M)
    • where Obj(M) is type of objects of the class being defined.

Losing Information

But now we have lost information.

  • If C has method^ m()^ with body^ return this,

then C m().

If D extends C, then inherited m still has

type C m().

  • But^ d.m()^ really returns value of type^ D!

37

Meaning of ThisClass

  • ThisClass^ is type of^ this.

In semantics:

  • Node = !ThisClass.Nd(ThisClass)
  • (^) DbleNode = !ThisClass'.DbNd(ThisClass');

Thus

  • ThisClass = Nd(ThisClass) = Node
  • ThisClass' = DbNd(ThisClass') = DoubleNode

38

Type-Checking

Wish to type-check modularly.

  • no repeated type-checking of inherited methods
  • Type-check methods of class C under

assumptions that hold in all extensions!

this : ThisClass

ThisClass extends C

Can prove soundness of type system.

39

Verification

  • Verification can benefit from similar

assumptions on this and ThisClass

  • Alternative:^ Verify under closed-world

assumption and copy inherited methods

down.

  • Compositional approach is better!

40

  • Gets^ much^ more interesting when:
    • Allow type parameters (e.g., GJ)
    • Introduce ThisType as interface of this.
      • LOOJ extension of GJ (Java 5)
    • Allow mutually recursive ThisClasses in class

groups.

  • Allows types and classes to be refined

simultaneously in type-safe fashion.

There Is Much More ...

Subject-Observer Example

SubjObsGrp = ClassGroup {

class MySubject {

void addObserver(MyObserver myObs) {...};

void notifyObservers: proc(MyEvent myEvt) {...}; }

class MyObserver {

void notify(MySubject mySubj, MyEvent myEvt) {...}; }

class MyEvent {…}

}

Extending Class Groups

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

Summary

Fixed points play key role in OO languages.

• Classes are generators of fixed points:

- cl = " this. CL(this)

- obj =! this. CL(this)

• Types similar:

- Class = " ThisClass. CLType(ThisClass)

- ObjTp =! ThisClass. CLType(ThisClass)

44

Summary ( continued )

Type-checking must take into consideration

future subclasses.

Verification should also plan for subclasses.

45

Summary ( continued )

Interdependent groups of objects common.

Need to subclass simultaneously

- Not supported in current languages

- Mutually recursive ThisClasses provide

opportunities for type-safe specialization.

46

References

• LOOJ:^ Weaving LOOM into Java, ECOOP^ 2004.

• Some Challenging^ Typing Issues in Object-Oriented

Languages, ENTCS 82, #8, 2003.

Foundations of Object-Oriented Languages: Types

and Semantics, MIT Press, 2002.

http://www.cs.williams.edu/~kim

Questions?