Title: CS412/413
1CS412/413
- Introduction to
- Compilers and Translators
- March 17, 1999
- Lecture 20 Implementing Objects
2Administration
- Programming Assignment 3, Part Idue Friday
- Reading Appel, Chapter 14
3Outline
- Checking methods
- Why Java arrays are broken
- Compiling methods
- Inheritance
- Compiling fields
- Multiple inheritance
- of interfaces
- of implementations
4Signature conformance
- Subclass method signatures must conform to those
of superclass - Argument types
- Return type
- Exceptions
- How much conformance is really needed?
- Java rule arguments and returns must be
identical, may remove exceptions
5Checking conformance
- Last time mutable fields of a record must be
invariant, immutable fields may be covariant - Object is essentially a record where methods are
immutable fields! - Type of method fields is a function type (T1?T2
?T3 ? Tn) - Subtyping rules for function types will tell us
subtyping rules for methods
6Function type subtyping
- class Shape
- int setLLCorner(Point p)
-
- class ColoredRectangle extends Shape
- int setLLCorner(ColoredPoint p)
-
- Ignoring Java overloading rules, would this
signature refinement be safe? (Eiffel) - Question ColoredPoint ? int ? Point ? int ?
7Testing function subtypes
- If ColoredPoint ? int ? Point ? int , must be
able to use former in place of latter. - int f(Point x) // f Point ? int
- int g(ColoredPoint x)
- f g // ok?
-
- g f // ok?
8Contravariance/covariance
- Function arguments may be contravariant
- Function results may be covariant
- ?i Ti ? Ti
- Tr ? Tr
- T1?...?Tn?Tr ? T1?...?Tn ? Tr
- Java is conservative!
9Java arrays
- Java has array type constructor for any type T,
T is an array of Ts - Java also has subtype rule
- T1 ? T2
- T1 ? T2
- Is this rule safe?
10Java array subtype problems
- Elephant ? Animal
- Animal x
- Elephant y
- x y
- x0 new Rhinoceros() // oops!
- Assignment as method
- void setElem (Animal element, int index)
- void setElem (Elephant element, int index)
11Compiling methods
- Methods look like functions, type like
functionswhat is different? - Call sequence use dispatch vector
- Argument list implicit receiver
12Method dispatch
- Idea every method has its own small integer
index - Index is used to look up method in dispatch vector
class C implements B void quux() 3
interface A void foo() 0 interface B
extends A void bar() 1 void baz() 2
A
B
C
13Dispatch vector layouts
foo
A
foo
B
bar
baz
foo
C
A foo
bar
B bar,baz
baz
C quux
quux
14Call sequence
function
method obj.baz()
f(...)
CALL
CALL
MEM
NAME(f)
i 4 ( 24)
MEM
obj
15Method arguments
- Methods have a special variable (in Java, this)
that is called the receiver object - History (Smalltalk) methods thought of as
messages sent to receivers - Receiver object is really an argument to the
method - class Shape
- int setCorner(int which, Point p)
-
-
int setCorner(Shape this, int which, Point p)
16Static methods
- In Java, can declare methods static -- they have
no receiver object - Exactly like normal functions
- dont need to enter into dispatch vector
- dont need implicit extra argument for receiver
- Treated as methods as way of getting functions
inside the class scope -- not really methods
17Inheritance
- Three traditional components of object-oriented
languages - abstraction/encapsulation/information hiding
- subtyping/interface inheritance -- interfaces
inherit method signatures from supertypes - inheritance/implementation inheritance -- a class
inherits signatures and code from a superclass
(possibly abstract)
18Inheritance
- Method code copied down from superclass if not
overridden by this class. - Fields also inherited (needed by inherited code
in general) - Fields checked just as for records mutable
fields must be invariant, immutable fields may be
covariant
19Object Layout
class Shape Point LL, UR void
setCorner(int which, Point p) class
ColoredRect extends Shape Color c void
setColor(Color c_)
DV
setCorner
LL Point
setColor
UR Point
c Color
Shape
ColoredRect
20Code Sharing
Machine code for Shape.setCorner
DV
setCorner
LL Point
UR Point
- Dont actuallyhave to copy code
- Can inherit withoutsuperclass source
DV
setCorner
LL Point
setColor
UR Point
c Color
21Field Offsets
- class Shape
- Point LL /4/ , UR /8/
- void setCorner(int which, Point p)
-
- class ColoredRect extends Shape
- Color c / 12 /
- void setColor(Color c_)
-
- Can figure out offsets of fields from beginning
of object statically - Accesses to fields are indexed loads
- ColoredRect x
- Tx.c MEM(Tx 12)
- Tx.UR MEM(Tx 8)
22Field Alignment
- In many processors, a 32-bit load must be made to
an address divisible by 4, 64-bit load address
must be divisible by 8. - In rest (e.g. Pentium), loads are faster if
aligned -- avoids extra load - Fields should be aligned
x
c
y
struct int x char c int y char d int z
double e
d
z
e
23Summary
- Rules for method conformance same as function
subtyping rules - covariant result type
- contravariant argument types
- Java subtype checking is overly strict
- Java arrays are not type-safe need run-time
checking - Method dispatch accomplished using dispatch
vector, implicit method receiver argument - No dispatch of static methods needed
- Inheritance causes extension of fields as well as
methods code can be shared - Field alignment declaration order matters!