Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10: Part 1: OO Issues CS 540 George Mason University.

Similar presentations


Presentation on theme: "Lecture 10: Part 1: OO Issues CS 540 George Mason University."— Presentation transcript:

1 Lecture 10: Part 1: OO Issues CS 540 George Mason University

2 CS 540 Spring 2004 GMU Object-Oriented Languages? What is an OOL? A language that supports “object-oriented programming” Term is almost meaningless today — Smalltalk to C++ to Java How does an OOL differ from an imperative language? Complex type system including inheritance and polymorphism. Object representation issues Resolution of names to their implementations

3 CS 540 Spring 2004 GMU An object is an abstract data type that encapsulates data, operations and internal state behind a simple, consistent interface. Elaborating the concepts: Each object needs local storage for its attributes –Attributes are static (lifetime of object ) –Access is through methods Some methods are public, others are private Object’s internal state leads to complex behavior Data Code x Data Code y Data Code z The Concept:

4 CS 540 Spring 2004 GMU Implementing Object-Oriented Languages Two critical issues in OOL implementation: Object representation Mapping a method invocation name to a method implementation These both are intimately related to the OOL’s name space Object Representation Static, private storage for attributes & instance variables –Heap allocate object records or “instances” Need consistent, fast access –Known, constant offsets Provision for initialization in NEW

5 CS 540 Spring 2004 GMU Simplistic method: Object Representation f1 code f2 code b: c: z f1 f2 f1 code f2 code b: c: z f1 f2 Each object gets copies of all attributes and methods Class A { int b,c; A z; f1() f2() } For object x of type A:

6 CS 540 Spring 2004 GMU Better method b: c: z f1 f2 b: c: z f1 f2 Objects share methods (and static attributes) Class A { int b,c; A z; f1() f2() } For object x of type A: f1 code f2 code

7 CS 540 Spring 2004 GMU More typically: b: c: z Objects share methods (and static attributes) via shared class object (can keep counter of objects N) b: c: z N: 2 d: f1 f2 parent class Class A { int b,c; static int d A z; f1() f2() } For object x of type A: f1 code f2 code Class A

8 CS 540 Spring 2004 GMU OOL Storage Layout Class variables Static class storage accessible by global name (class C) –Method code put at fixed offset from start of class area –Static variables and class related bookkeeping Object Variables Object storage is heap allocated at object creation –Fields at fixed offsets from start of object storage Methods –Code for methods is stored with the class –Methods accessed by offsets from code vector Allows method references inline –Method local storage in object (no calls) or on stack

9 CS 540 Spring 2004 GMU Dealing with Single Inheritance Use prefixing of storage for objects Class Point { int x, y; } Class ColorPoint extends Point { Color c; } x y x y c self

10 CS 540 Spring 2004 GMU Object-Oriented Languages - Dispatching Mapping message names to methods Static mapping, known at compile-time (Java, C++) –Fixed offsets & indirect calls Dynamic mapping, unknown until run-time (Smalltalk, C++ with pointers) –Look up name in class’ table of methods This is really a data-structures problem Build a table of function pointers Use a standard invocation sequence

11 CS 540 Spring 2004 GMU Single Inheritance and Dynamic Dispatch Class Point { int x, y; public void draw(); public void d2o(); } Class ColorPoint extends Point { Color c; public void draw(); public void rev(); } x y table draw d2o draw d2o table rev Point: draw ColorPoint:draw ColorPoint: rev Point: d20 self x y c

12 CS 540 Spring 2004 GMU Multiple Inheritance The idea Allow more flexible sharing of methods & attributes Relax the inclusion requirement If B is a subclass of A, it need not implement all of A’s methods Need a linguistic mechanism for specifying partial inheritance Problems when C inherits from both A & B C’s method table can extend A or B, but not both –Layout of an object record for C becomes tricky Other classes, say D, can inherit from C & B –Adjustments to offsets become complex Both A & B might provide fum() — which is seen in C ? –C++ produces a “syntax error” when fum() is used


Download ppt "Lecture 10: Part 1: OO Issues CS 540 George Mason University."

Similar presentations


Ads by Google