Post on 16-Oct-2020
CSE 130 : Fall 2008
Programming Languages
Lecture 16:St ti T f Obj tStatic Types for Objects
Ranjit JhalaUC San Diego
Quiz
Last timeLast time
• Tricks with namespaces: decorators • Tricks with namespaces: decorators …
TodayToday
• Inheritance• Inheritance
S i T f Obj • Static Types for Objects
What is a type?What is a type?
What is a type?What is a type?
• A description of the actions that one can A description of the actions that one can successfully perform on an object
• A description of an object: – fields it containsfields it contains– methods it contains
• Type = attributes an object contains– “attribute” recursively includes its typeattribute recursively includes its type
Types for Objects: InterfacesTypes for Objects: Interfaces
Interface: List of attributes (with types)
interface Point {
Interface: List of attributes (with types)
{double getX();double getY();void move(double dx double dy)void move(double dx, double dy)void jump(double x, double y)void draw(Screen s)
}
ExampleExample
interface Point { set of objects with attrs:
The following type: Corresponds to:
interface Point {double getX();double getY();void move(double dx, double dy)
set of objects with attrs: getX getY move
void jump(double x, double y)void draw(Screen s)
}
jump draw
Object may have many Object may have many other attributes!
void screenSaver(Point [] p) {Compiler checks that screenSaver only uses known attributes of Point
}known attributes of Point
Clients of screenSaverClients of screenSaver
What objects can be passed to screenSaver?What objects can be passed to screenSaver?
Any object w/ Point’s attributes • Any object w/ Point s attributes
N bl if bj t h tt ib t• No problem if object has more attributes
• This idea is called subtyping
Subtype Polymorphism Subtype Polymorphism
“T1 is a subtype of type T2 ”T1 is a subtype of type T2
If h bj f T b • If wherever an object of type T2 can be used, so can an object of type T1
• If T1 <: T2 then wherever an object of 1 2 jtype T2 is required, you can safely pass in an object of type T1j yp 1
SubtypingSubtyping
When can we say T1 <: T2 ?When can we say T1 <: T2 ?
Wh bj f T h When an object of type T1 has all the attributes of an object of type T2
Subtyping with types-as-setsSubtyping with types as sets
If: If: 1. T1 <: T2
2 objs(T ) = set of objects of type T2. objs(T1) = set of objects of type T1
3. objs(T2) = set of objects of type T2
th h bj (T ) d bj (T ) l t d?then how are objs(T1) and objs(T2) related?
T
Ans: obs(T1) ⊆ objs(T2)T2
T1
Example of subtypingExample of subtypinginterface Point {
double getX();Recall:
double getX();double getY();void move(double dx, double dy)void jump(double x, double y)
id d (S )void draw(Screen s)}
interface ColorPoint { interface TextPoint {interface ColorPoint {double getX();double getY();void move(double dx, double dy)
interface TextPoint {double getX();double getY();void move(double dx, double dy)
void jump(double x, double y)void draw(Screen s)void setColor(Color c)Color getColor()
void jump(double x, double y)void draw(Screen s)void setText(string s)string getText()Color getColor()
}string getText()
}
Example of subtypingExample of subtyping
objects
Point
ColorPoint TextPoint
Another exampleAnother example
interface ColorTriangle {interface Triangle { void foo(Triangle t)g {ColorPoint p1;ColorPoint p2;ColorPoint p3;
}
g {Point p1;Point p2;Point p3;
}
( g )
I C l T i l T i l ?
}}
Is ColorTriangle <: Triangle ?• Is it the case that wherever a Triangle is
expected it is safe to pass in a ColorTriangle ?expected, it is safe to pass in a ColorTriangle ?• Is it safe to pass ColorTriangle to foo ?
Another exampleAnother example
void foo(Triangle t)interface ColorTriangle {interface Triangle { ( g )g {ColorPoint p1;ColorPoint p2;ColorPoint p3;
}
g {Point p1;Point p2;Point p3;
}
S f t C l T i l t f ?
}}
• Safe to pass ColorTriangle to foo?• Suppose foo only reads fields of t
– Then it’s safe.
• What if foo writes fields of t ?
Another exampleAnother example
void foo(Triangle t) {interface ColorTriangle {interface Triangle { ( g ) {t.p1 := new Point()
}
g {ColorPoint p1;ColorPoint p2;ColorPoint p3;
}
g {Point p1;Point p2;Point p3;
} t C l T i l ()}} t := new ColorTriangle();foo(t);t.p1.color // yikes!
SSummary:• interface A is a subtype of interface B• if attributes of A and B match
…but its tricky! details in CSE 230
What happens in Java ?What happens in Java ?
How does Java figure out if How does Java figure out if one interface is a subtype of another?
• Looks at the subclass relationship!
Structural vs. Nominal subtypingStructural vs. Nominal subtyping• Structural subtyping
– subtyping based on type structure (attributes)
• Nominal subtyping– subtyping “by name”– subtyping relation given by programmer
• Java: – Interface = Type– Class = Type + Implementation– i.e. Class automatically defines an interface
Programmer declares SubtypingProgrammer declares Subtyping
ColoredPoint <: Pointclass ColoredPoint implements Point {
}
ColoredPoint <: Pointclass ColoredPoint extends Point {
}
Whats the difference ?Whats the difference ?
ColoredPoint <: Pointclass ColoredPoint implements Point {
Compiler checks ll b f
}
all attributes of Point are defined in ColorPoint
Subtyping
ColoredPoint <: Pointclass ColoredPoint extends Point {
Compiler includes all attributes of Point
}
I h it all attributes of Point in ColorPoint
Inheritance
Subtype Polymorphism in JavaSubtype Polymorphism in Java• How to get polymorphic lists in Java ?
interface List {void add(Object o);Object get(int i);
• But
Object get(int i);}
• But …List l = … ;l.add(“persnickety”);MString s = l.get(0); Compiler
Grumbles
Lost information with supertype “object”
A Hack : CastingA Hack : CastingList l = … ;l.add(“persnickety”);MString s = (string) l.get(0);String s (string) l.get(0);Int i = (Int) l.get(0); Compiler
happy but…
Unsafe: runtime error!
Generics: ML-style Polymorphism Generics: ML style Polymorphism
• How to get polymorphic lists in Java ?interface List <T> {void add(T o);T get(int i);
Generalize
• Now
T get(int i);}
• Now …List <string> l = … ;l.add(“persnickety”);
Instantiate( p y )
MString s = l.get(0);l add(223);
Safel.add(223);
Compile time error
Bounded Polymorphism in JavaBounded Polymorphism in Java• How to get polymorphic “drawable” lists in Java ?
interface Drawable {void draw();
}
BoundedGeneralize∀ T<: Drawable
interface DList <T extends Drawable>{void add(T o);T get(int i);
DList <circle> l = … ; Instantiate Check :
T get(int i);}
;l.add(/* circle obj */);MCircle c = l.get(0);
Safe
Instantiate Check : <: Drawable
CSE 230, Winter 07
Square s = l.get(0); SafeCompile time error
That’s all for objectsThat s all for objects
Good luck with PA 6Good luck with PA 6
N t k P lNext week: Prolog
Happy Thanksgiving!