340 likes | 469 Views
Languages and Compilers (SProg og Oversættere). Bent Thomsen Department of Computer Science Aalborg University. With acknowledgement to John Mitchell who’s slides this lecture is based on. Varieties of OO languages. class-based languages behavior of object determined by its class
E N D
Languages and Compilers(SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchellwho’s slides this lecture is based on.
Varieties of OO languages • class-based languages • behavior of object determined by its class • object-based • objects defined directly • multi-methods • operation depends on all operands
History • Simula 1960’s • Object concept used in simulation • Smalltalk 1970’s • Object-oriented design, systems • C++ 1980’s • Adapted Simula ideas to C • Java 1990’s • Distributed programming, internet
hidden data msg1 method1 . . . . . . msgn methodn Objects • An object consists of • hidden data instance variables, also called member data hidden functions also possible • public operations methods or member functions can also have public variables in some languages • Object-oriented program: • Send messages to objects
What’s interesting about this? • Universal encapsulation construct • Data structure • File system • Database • Window • Integer • Metaphor usefully ambiguous • sequential or concurrent computation • distributed, sync. or async. communication
Object-oriented programming • Programming methodology • organize concepts into objects and classes • build extensible systems • Language concepts • encapsulate data and functions into objects • subtyping allows extensions of data types • inheritance allows reuse of implementation • dynamic lookup
Dynamic Lookup • In object-oriented programming, object message (arguments) object.method(arguments) code depends on object and message • In conventional programming, operation (operands) meaning of operation is always the same
Example • Add two numbers x add (y) differentaddif xis integer, complex • Conventional programming add (x, y) functionadd has fixed meaning Important distinction: Overloading is resolved at compile time, Dynamic lookup at run time.
Encapsulation • Builder of a concept has detailed view • User of a concept has “abstract” view • Encapsulation is the mechanism for separating these two views message Object
Comparison • Traditional approach to encapsulation is through abstract data types • Advantage • Separate interface from implementation • Disadvantage • Not extensible in the way that OOP is We will look at ADT’s example to see what problem is
Abstract data types abstype q with mk_Queue : unit -> q is_empty : q -> bool insert : q * elem -> q remove : q -> elem is … in program end
Priority Q, similar to Queue abstype pq with mk_Queue : unit -> pq is_empty : pq -> bool insert : pq * elem -> pq remove : pq -> elem is … in program end But cannot intermix pq’s and q’s
Abstract Data Types • Guarantee invariants of data structure • only functions of the data type have access to the internal representation of data • Limited “reuse” • Cannot apply queue code to pqueue, except by explicit parameterization, even though signatures identical • Cannot form list of points, colored points • Data abstraction is important part of OOP, innovation is that it occurs in an extensible form
Subtyping and Inheritance • Interface • The external view of an object • Subtyping • Relation between interfaces • Implementation • The internal representation of an object • Inheritance • Relation between implementations
Object Interfaces • Interface • The messages understood by an object • Example: point • x-coord : returns x-coordinate of a point • y-coord : returns y-coordinate of a point • move : method for changing location • The interface of an object is its type.
Subtyping • If interface A contains all of interface B, then A objects can also be used B objects. Point x-coord • y-coord • move Colored_point x-coord • y-coord • color • move • change_color • Colored_point interface contains Point • Colored_point is a subtype of Point
Inheritance • Implementation mechanism • New objects may be defined by reusing implementations of other objects
Example class Point private float x, y public point move (float dx, float dy); class Colored_point private float x, y; color c public point move(float dx, float dy); point change_color(color newc); • Subtyping • Colored points can be used in place of points • Property used by client program • Inheritance • Colored points can be implemented by resuing point implementation • Propetry used by implementor of classes
OO Program Structure • Group data and functions • Class • Defines behavior of all objects that are instances of the class • Subtyping • Place similar data in related classes • Inheritance • Avoid reimplementing functions that are already defined
Example: Geometry Library • Define general concept shape • Implement two shapes: circle, rectangle • Functions on implemented shapes center, move, rotate, print • Anticipate additions to library
Shapes • Interface of every shape must include center, move, rotate, print • Different kinds of shapes are implemented differently • Square: four points, representing corners • Circle: center point and radius
Subtype hierarchy • General interface defined in the shape class • Implementations defined in circle, rectangle • Extend hierarchy with additional shapes Shape Circle Rectangle
Code placed in classes • Dynamic lookup • circle move(x,y) calls function c_move • Conventional organization • Place c_move, r_move in move function
Example use: Processing Loop Remove shape from work queue Perform action Control loop does not know the type of each shape
Subtyping differs from inheritance Collection Indexed Set Array Dictionary Sorted Set String Subtyping Inheritance
access link real x 1.0 real y 2.5 proc equals proc distance Representation of objects Object is represented by activation record with access link to find global variables according to static scoping p code for equals code for distance
Run-time representation of point to superclass Object Point class Template Point object x class y x 3 y 2 Method dictionary code newX:Y: ... ... Detail: class method shown in dictionary, but lookup procedure distinguishes class and instance methods move code
Access Control • In many OOPLs it is possible to declare attributes (and methods) private or public or protected etc. • This has no effect on the running program, but simply means that the compiler will reject programs which violate the access-rules specified • The control is done as part of static semantic analysis
Dynamic (late) Binding • Consider the method call: • x.f(a,b) where is f defined? in the class (type)of x? Or in a predecessor? • If multiple inheritance is supported then the entire predecessor graph must be searched: • This costs a large overhead in dynamic typed languages like Smalltalk (normally these languages don’t support multiple inheritance) • In static typed languages like Java, Eiffel, C++ the compiler is able to analyse the class-hierarchy (or more precise: the graph) for x and create a display-array containing addresses for all methods of an object (including inherited methods) • According to Meyer the overhead of this compared to static binding is at most 30%, and overhead decreases with complexity of the method
Multiple Inheritance • In the case of simple inheritance, each class may have one direct predecessor; multiple inheritance allows a class to have several direct predecessors. • In this case the simple ways of accessing attributes and binding method-calls (shown previously) don’t work. • The problem: if class C inherits class A and class B the objects of class C cannot begin with attributes inherited from A and at the same time begin with attributes inherited from B. • In addition to these implementation problems multiple inheritance also introduces problems at the language (conceptual) level.
Attribute Offset with Multiple Inheritance • The problem is actually a graph colouring problem: • let each name be represented by a vertex • if two names are in the same scope (perhaps through inheritance) then they are connected by an edge • all vertices which are adjacent must have different offsets • this approach may cause empty slots in an object (fig. 14.4) • the offsets (and the waste of space) may be moved to the class-descriptor (and there much more objects than classes) costing an overhead of two instructions per fetch or store (fig. 14.5) • this analysis can be done compile-time • a similar technique may be used for dynamic binding of methods
Implementation of Object Oriented Languages • Implementation of Object Oriented Languages differs only slightly from implementations of block structured imperative languages • Some additional work to do for the contextual analysis • Access control, e.g. private, public, procted directives • Subtyping can be tricky to implement correctly • The main difference is that methods usually have to be looked up dynamically, thus adding a bit of run-time overhead • Only multiple inheritance poses a bigger problem