DOC PREVIEW
UA CSC 520 - OO Languages — Introduction

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Object-Oriented LanguagesObject-Oriented Languagesldots Compiling OO LanguagesObject-Oriented ExampleExample in JavaExample in Modula-3 (A)Example in Modula-3 (B)Example in Oberon-2Record LayoutRecord LayoutRecord Layoutldots Record Layoutldots Class TemplatesClass Templatesldots Class Templatesldots Method InvocationMethod Invocationldots Exam ProblemExam Problem Ildots Readings and ReferencesSummarySummaryldots Confused Student Email520—Spring 2005—45CSc 520Principles of ProgrammingLanguages45: OO Languages — IntroductionChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2005 Christian Collberg[1]520—Spring 2005—45Object-Oriented LanguagesObject-oriented languages extend imperativelanguages with:1. A classification scheme that allows us to specify is-aas well as has-a relationships. Has-a is supported byPascal, where we can declare that one data item hasanother item (a record variable has-a record field).Object-Pascal, Oberon, etc, extends this capabilitywith inheritance which allows us to state that one dataitem is (an extension of) another item.2. Late binding, which allows us to select betweendifferent implementations of the same abstract datatype at run-time.[2]520—Spring 2005—45Object-Oriented Languages...3. Polymorphism, which is the ability of a variable tostore values of different types. OO languagessupport a special kind of polymorphism, calledinclusion polymorphism, that restricts the values thatcan be stored in a variable of typeT to values oftype T or subtypes of T .4. Data encapsulation. Data (instance variables) andoperations (methods) are defined together.5. Templates and objects. A template (class orprototype) describes how to create new objects(instances of abstract data types).[3]520—Spring 2005—45Compiling OO LanguagesRuntime type checking (a variable of type ref T mayonly reference objects of typeT or T ’s subtypes).Because of the polymorphic nature of OO languages,we can’t always know (at compile-time) the type of theobject that a given variable will refer to at run-time.When we invoke a method we can’t actually know whichpiece of code we should execute. Finding the rightpiece of code is calledmethod lookup. It can be done byname (Objective-C) or number (C++).Most OO languages rely on dynamic allocation.Garbage collection is a necessary part of the runtimesystem of a compiler for an OO language (C++non-withstanding). This requiresruntime type description.[4]520—Spring 2005—45Object-Oriented ExampleTYPE Shape = CLASSx, y : REAL;METHOD draw(); BEGIN · · ·; END;METHOD move(X,Y:REAL); BEGIN x := x+X; END;END;TYPE Square = Shape CLASSside : REAL;METHOD draw(); BEGIN · · ·; END;END;TYPE Circle = Shape CLASSradius : REAL;METHOD draw(); BEGIN · · ·; END;METHOD area():REAL; BEGIN · · · END;END;[5]520—Spring 2005—45Example in Java// Example in Javaclass Shape {double x, y;void draw(); { · · · }void move(double X, double Y); {x = x+X; }}class Square extends Shape {double side;void draw(); { · · ·}}class Circle extends Shape {double radius;void draw(); { · · · }double area(); { · · · }}[6]520—Spring 2005—45Example in Modula-3 (A)(* Example in Modula-3 *)TYPE Shape = OBJECTx, y : REALMETHODSdraw() := DefaultDraw; move(X, Y : REAL) := Move;END;Square = Shape OBJECTside : REALMETHODSdraw() := SquareDrawEND;Circle = Shape OBJECTradius : REALMETHODSdraw() := CirlceDraw; area() := ComputeAreaEND;[7]520—Spring 2005—45Example in Modula-3 (B)(* Example in Modula-3 (continued) *)PROCEDURE Move (Self : Shape; X, Y : REAL) =BEGIN · · · END Move;PROCEDURE DefaultDraw (Self : Shape) =BEGIN · · · END DefaultDraw;PROCEDURE SquareDraw (Self : Square) =BEGIN · · · END SquareDraw;PROCEDURE CircleDraw (Self : Circle) =BEGIN · · · END CircleDraw;PROCEDURE ComputeArea (Self : Circle) : REAL =BEGIN · · · END ComputeArea;[8]520—Spring 2005—45Example in Oberon-2TYPE Shape = RECORD x, y : REAL END;Square = RECORD (Shape) side : REAL END;Circle = RECORD (Shape) radius : REAL END;PROCEDURE (Self : Shape) Move (X, Y : REAL) =BEGIN · · · END Move;PROCEDURE (Self : Shape) DefaultDraw () =BEGIN · · · END DefaultDraw;PROCEDURE (Self : Square) SquareDraw () =BEGIN · · · END SquareDraw;PROCEDURE (Self : Circle) CircleDraw () =BEGIN · · · END CircleDraw;PROCEDURE (Self : Circle) ComputeArea () : REAL =BEGIN · · · END ComputeArea;[9]520—Spring 2005—45Record LayoutSingle inheritance is implemented by concatenation, i.e.the instance variables of classC are1. the variables ofC’s supertype, followed by2. the variables thatC declares itself.RecordLayoutInheritanceHierarchyC1’s instance varsC1’s instance varsC2’s instance varsC1’s instance varsC2’s instance varsC3’s instance varsC1C2C3[10]520—Spring 2005—45Record LayoutThe offsets of the variables that C inherits from itssupertype will be the same as in the supertype itself.In this example, C3inherits from C2which inherits fromC1.C3will have the fields from C1followed by the fields fromC2followed by C3’s own fields. The order is significant.RecordLayoutInheritanceHierarchyC1’s instance varsC1’s instance varsC2’s instance varsC1’s instance varsC2’s instance varsC3’s instance varsC1C2C3[11]520—Spring 2005—45Record Layout...TYPE Shape =CLASS x,y: REAL; END;TYPE Square = ShapeCLASS side:REAL; END;TYPE Circle = ShapeCLASS radius:REAL; END;VAR S:Shape;VAR Q:Square;VAR C:Circle;ShapeSquareCircleInheritanceHierarchySQCx:REALy:REALx:REALy:REALside:REALx:REALy:REALradius:REAL[12]520—Spring 2005—45Record Layout...An OO language compiler would translate thedeclarations in the previous slide into something similarto this:TYPE Shape=POINTER TO RECORDx, y: REAL;END;TYPE Square=POINTER TO RECORDx, y: REAL;side:REAL;END;TYPE Circle=POINTER TO RECORDx, y: REAL;radius:REAL;END;VAR S:Shape; Q:Square; C:Circle;[13]520—Spring 2005—45Class TemplatesTo support late binding, runtime typechecking, etc, eachclass is represented by a template at runtime. Eachtemplate has pointers to the class’s methods andsupertype.Codeformethodsdraw:move:parent:Square$Templatedraw:move:area:parent:Circle$TemplateShape$Templatedraw:move:parent:ROOTSquare$drawCircle$drawShape$drawCircle$areaShape$move[14]520—Spring 2005—45Class Templates...Square’s x,y fields are inherited from Shape. Their offsets are thesame as inShape.TYPE $TemplateT=POINTER TO RECORDparent : $TemplateT;move :ADDRESS;draw :


View Full Document

UA CSC 520 - OO Languages — Introduction

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download OO Languages — Introduction
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view OO Languages — Introduction and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view OO Languages — Introduction 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?