CSE 130 : Fall 2008 Programming Languages Quiz Lecture 16: Static ti Types for Objects Ranjit Jhala UC San Diego Last time Tricks with namespaces: decorators Today Inheritance Static Types for Objects
What is a type?
What is a type? A description of the actions that one can successfully perform on an object A description of an object: fields it contains methods it contains Type = attributes an object contains attribute recursively includes its type Types for Objects: Interfaces Interface: List of attributes (with types) interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) Example Clients of screensaver The following type: interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) Corresponds to: set of objects with attrs: getx gety move jump draw Object may have many other attributes! What objects can be passed to screensaver? Any object w/ Point s attributes No problem if object has more attributes t void screensaver(point [] p) { Compiler checks that screensaver only uses known attributes of Point This idea is called subtyping
Subtype Polymorphism T 1 is a subtype of type T 2 Subtyping When can we say T 1 <: T 2? If wherever an object of type T 2 can be used, so can an object of type T 1 When an object of type T 1 has all the attributes of an object of type T 2 If T 1 <: T 2 then wherever an object of type T 2 is required, you can safely pass in an object of type T 1 Subtyping with types-as-sets sets If: 1. T 1 <: T 2 2. objs(t 1 ) = set of objects of type T 1 3. objs(t 2 ) = set of objects of type T 2 then how are objs(t 1 ) and objs(t 2 ) related? Ans: obs(t 1 ) objs(t 2 ) T 1 T 2 Example of subtyping Recall: interface Point { double getx(); double gety(); void move(double dx, double dy) void jump(double x, double y) void draw(screen s) interface ColorPoint { interface TextPoint { double getx(); double getx(); double gety(); 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 move(double dx, double dy) void jump(double x, double y) void draw(screen s) void settext(string s) string gettext()
Example of subtyping Another example objects Point interface Triangle { interface ColorTriangle { void foo(triangle t) Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; ColorPoint TextPoint Is ColorTriangle l <: Triangle? Is it the case that wherever a Triangle is expected, it is safe to pass in a ColorTriangle? Is it safe to pass ColorTriangle to foo? Another example Another example interface Triangle { interface ColorTriangle { void foo(triangle t) Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; Safe to pass ColorTriangle l to foo? Suppose foo only reads fields of t Then it s safe. What if foo writes fields of t? interface Triangle { interface ColorTriangle { void foo(triangle t) { Point p1; Point p2; Point p3; ColorPoint p1; ColorPoint p2; ColorPoint p3; t.p1 := new Point() t := new ColorTriangle(); l foo(t); t.p1.color // yikes! Summary: 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? How does Java figure out if one interface is a subtype of another? Looks at the subclass relationship! Structural 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 Subtyping Whats the difference? class ColoredPoint implements Point { ColoredPoint <: Point class ColoredPoint implements Point { ColoredPoint <: Point Subtyping Compiler checks all attributes of Point are defined in ColorPoint class ColoredPoint extends Point { ColoredPoint <: Point class ColoredPoint extends Point { ColoredPoint <: Point Compiler includes all attributes of Point in ColorPoint Inheritance all attributes of Point
Subtype Polymorphism in Java A Hack : Casting How to get polymorphic lists in Java? interface List { void add(object o); Object get(int i); But List l = ; l.add( persnickety ); M String s = l.get(0); Compiler Grumbles List l = ; l.add( persnickety ); M String s = (string) l.get(0); Int i = (Int) l.get(0); Unsafe: runtime error! Compiler happy but Lost information with supertype object Generics: ML-style Polymorphism How to get polymorphic lists in Java? Now interface List <T> { void add(t o); T get(int i); List <string> l = ; l.add( persnickety ); M String s = l.get(0); l.add(223); Instantiate Safe Generalize Compile time error Bounded Polymorphism in Java How to get polymorphic drawable lists in Java? interface Drawable { void draw(); interface DList <T extends Drawable>{ void add(t o); T get(int i); l.add(/* circle obj */); M Circle c = l.get(0); BoundedGeneralize T<: Drawable DList <circle> l = ; Instantiate Check : <: Drawable Square s = l.get(0); CSE 230, Winter 07 Safe Compile time error
That s all for objects Good luck with PA 6 Next week: Prolog Happy Thanksgiving!