Concepts in ObjectOriented Programming Languages - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Concepts in ObjectOriented Programming Languages

Description:

Designers: Dahl, Myhrhaug, Nygaard. Simula-1 in 1966 (strictly a simulation language) ... Dahl and Myhrhaug. Maintained concern for general programming ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 55
Provided by: JohnCMi2
Category:

less

Transcript and Presenter's Notes

Title: Concepts in ObjectOriented Programming Languages


1
Concepts in Object-Oriented Programming Languages
  • Peter's and my presentation differed
    substantially from these slides. I just post
    them for your convenience.
  • most slides stolen from John Mitchell (chapters
    10 11)

2
Outline of lecture
  • Object-oriented design
  • Primary object-oriented language concepts
  • dynamic lookup
  • encapsulation
  • inheritance
  • subtyping
  • Program organization
  • Work queue, geometry program, design patterns
  • Comparison
  • Objects as closures?

3
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

4
Whats 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

5
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

6
Object-oriented Method Booch
  • Four steps
  • Identify the objects at a given level of
    abstraction
  • Identify the semantics (intended behavior) of
    objects
  • Identify the relationships among the objects
  • Implement these objects
  • Iterative process
  • Implement objects by repeating these steps
  • Not necessarily top-down
  • Level of abstraction could start anywhere

7
This Method
  • Based on associating objects with components or
    concepts in a system
  • Why iterative?
  • An object is typically implemented using a number
    of constituent objects
  • Apply same methodology to subsystems, underlying
    concepts

8
Example Compute Weight of Car
  • Car object
  • Contains list of main parts (each an object)
  • chassis, body, engine, drive train, wheel
    assemblies
  • Method to compute weight
  • sum the weights to compute total
  • Part objects
  • Each may have list of main sub-parts
  • Each must have method to compute weight

9
Comparison to top-down design
  • Similarity
  • A task is typically accomplished by completing a
    number of finer-grained sub-tasks
  • Differences
  • Focus of top-down design is on program structure
  • OO methods are based on modeling ideas
  • Combining functions and data into objects makes
    data refinement more natural (I think)

10
Object-Orientation
  • Programming methodology
  • organize concepts into objects and classes
  • build extensible systems
  • Language concepts
  • dynamic lookup
  • encapsulation
  • subtyping allows extensions of concepts
  • inheritance allows reuse of implementation

11
Dynamic Lookup
  • In object-oriented programming,
  • object ? message (arguments)
  • code depends on object and message
  • In conventional programming,
  • operation (operands)
  • meaning of operation is always the same

Fundamental difference between abstract data
types and objects
12
Example
  • Add two numbers x ? add (y)
  • different add if x is integer, complex
  • Conventional programming add (x, y)
  • function add has fixed meaning
  • Very important distinction
  • Overloading is resolved at compile time,
  • Dynamic lookup at run time

13
Language concepts
  • dynamic lookup
  • different code for different object
  • integer different from real
  • encapsulation
  • subtyping
  • inheritance

14
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
15
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 ADTs example to see what problem
    is

16
Abstract data types
  • abstype q
  • with
  • mk_Queue unit -gt q
  • is_empty q -gt bool
  • insert q elem -gt q
  • remove q -gt elem
  • is
  • in
  • program
  • end

17
Priority Q, similar to Queue
  • abstype pq
  • with mk_Queue unit -gt pq
  • is_empty pq -gt bool
  • insert pq elem -gt pq
  • remove pq -gt elem
  • is
  • in
  • program
  • end
  • But cannot intermix pqs and qs

18
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

19
Language concepts
  • dynamic lookup
  • different code for different object
  • integer different from real
  • encapsulation
  • subtyping
  • inheritance

20
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

21
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.

22
Subtyping
  • If interface A contains all of interface B, then
    A objects can also be used as 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

23
Inheritance
  • Implementation mechanism
  • New objects may be defined by reusing
    implementations of other objects

24
Example
  • 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
  • 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)

25
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

26
Example Geometry Library
  • Define general concept shape
  • Implement two shapes circle, rectangle
  • Functions on implemented shapes
  • center, move, rotate, print
  • Anticipate additions to library

27
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

28
Subtype hierarchy
Shape
Circle
Rectangle
  • General interface defined in the shape class
  • Implementations defined in circle, rectangle
  • Extend hierarchy with additional shapes

29
Code placed in classes
  • Dynamic lookup
  • circle ? move(x,y) calls function c_move
  • Conventional organization
  • Place c_move, r_move in move function

30
Example use Processing Loop
  • Remove shape from work queue
  • Perform action

Control loop does not know the type of each shape
31
Subtyping differs from inheritance
Collection
Indexed
Set
Array
Dictionary
Sorted Set
String
Subtyping
Inheritance
32
Design Patterns
  • Classes and objects are useful organizing
    concepts
  • Culture of design patterns has developed around
    object-oriented programming
  • Shows value of OOP for program organization and
    problem solving

33
What is a design pattern?
  • General solution that has developed from
    repeatedly addressing similar problems.
  • Example singleton
  • Restrict programs so that only one instance of a
    class can be created
  • Singleton design pattern provides standard
    solution
  • Not a class template
  • Using most patterns will require some thought
  • Pattern is meant to capture experience in useful
    form
  • Standard reference Gamma, Helm, Johnson,
    Vlissides

34
OOP in Conventional Language
  • Records provide dynamic lookup
  • Scoping provides another form of encapsulation
  • Try object-oriented programming in ML.
  • Will it work? Lets see whats fundamental to
    OOP

35
Dynamic Lookup (again)
  • receiver ? operation (arguments)
  • code depends on receiver and operation
  • This is may be achieved in conventional
    languages using record with function components

36
Stacks as closures
  • fun create_stack(x)
  • let val store ref x in
  • push fn (y) gt
  • store y(!store),
  • pop fn () gt
  • case !store of
  • nil gt raise Empty
  • ym gt (store m y)
  • end
  • val stk create_stack(1)
  • stk popfn,pushfn popunit -gt int,
    pushint -gt unit

37
Does this work ???
  • Depends on what you mean by work
  • Provides
  • encapsulation of private data
  • dynamic lookup
  • But
  • cannot substitute extended stacks for stacks
  • only weak form of inheritance
  • can add new operations to stack
  • not mutually recursive with old operations

38
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
  • This course class-based languages

39
Summary
  • Object-oriented design
  • Primary object-oriented language concepts
  • dynamic lookup
  • encapsulation
  • inheritance
  • subtyping
  • Program organization
  • Work queue, geometry program, design patterns
  • Comparison
  • Objects as closures?

40
Example Container Classes
  • Different ways of organizing objects
  • Set unordered collection of objects
  • Array sequence, indexed by integers
  • Dictionary set of pairs, (word, definition)
  • String sequence of letters
  • Developed as part of Smalltalk system

41
Simula 67
  • First object-oriented language
  • Designed for simulation
  • Later recognized as general-purpose prog language
  • Extension of Algol 60
  • Standardized as Simula (no 67) in 1977
  • Inspiration to many later designers
  • Smalltalk
  • C
  • ...

42
Brief history
  • Norwegian Computing Center
  • Designers Dahl, Myhrhaug, Nygaard
  • Simula-1 in 1966 (strictly a simulation
    language)
  • General language ideas
  • Influenced by Hoares ideas on data types
  • Added classes and prefixing (subtyping) to Algol
    60
  • Nygaard
  • Operations Research specialist and political
    activist
  • Wanted language to describe social and industrial
    systems
  • Allow ordinary people to understand political
    (?) changes
  • Dahl and Myhrhaug
  • Maintained concern for general programming

43
Comparison to Algol 60
  • Added features
  • class concept
  • reference variables (pointers to objects)
  • pass-by-reference
  • char, text, I/O
  • coroutines
  • Removed
  • Changed default par passing from pass-by-name
  • some var initialization requirements
  • own (C static) variables
  • string type (in favor of text type)

44
Objects in Simula
  • Class
  • A procedure that returns a pointer to its
    activation record
  • Object
  • Activation record produced by call to a class
  • Object access
  • Access any local variable or procedures using dot
    notation object.
  • Memory management
  • Objects are garbage collected
  • user destructors considered undesirable

45
Example Circles and lines
  • Problem
  • Find the center and radius of the circle passing
    through three distinct points, p, q, and r
  • Solution
  • Draw intersecting circles Cp, Cq around p,q and
    circles Cq, Cr around q, r (Picture assumes Cq
    Cq)
  • Draw lines through circle intersections
  • The intersection of the lines is the center of
    the desired circle.
  • Error if the points are colinear.

q
p
r
46
Approach in Simula
  • Methodology
  • Represent points, lines, and circles as objects.
  • Equip objects with necessary operations.
  • Operations
  • Point
  • equality(anotherPoint) boolean
  • distance(anotherPoint) real (needed to
    construct circles)
  • Line
  • parallelto(anotherLine) boolean (to
    see if lines intersect)
  • meets(anotherLine) REF(Point)
  • Circle
  • intersects(anotherCircle) REF(Line)

47
Simula Point Class
  • class Point(x,y) real x,y
  • begin
  • boolean procedure equals(p)
    ref(Point) p
  • if p / none then
  • equals abs(x - p.x)
    abs(y - p.y) lt 0.00001
  • real procedure distance(p) ref(Point)
    p
  • if p none then error else
  • distance sqrt(( x - p.x
    )2 (y - p.y) 2)
  • end Point
  • p - new Point(1.0, 2.5)
  • q - new Point(2.0,3.5)
  • if p.distance(q) gt 2 then ...

48
Representation of objects
p
code for equals
code for distance
  • Object is represented by activation record
    with access link to find global variables
    according to static scoping

49
Simula line class
  • class Line(a,b,c) real a,b,c
  • begin
  • boolean procedure parallelto(l)
    ref(Line) l
  • if l / none then parallelto
    ...
  • ref(Point) procedure meets(l) ref(Line)
    l
  • begin real t
  • if l / none and parallelto(l)
    then ...
  • end
  • real d d sqrt(a2 b2)
  • if d 0.0 then error else
  • begin
  • d 1/d
  • a ad b bd c cd
  • end
  • end Line

Local variables line determined by axbyc0
Procedures
Initialization normalize a,b,c
50
Derived classes in Simula
  • A class decl may be prefixed by a class name
  • class A
  • A class B
  • A class C
  • B class D
  • An object of a prefixed class is the
    concatenation of objects of each class in prefix
  • d - new D()

A part
B part
D part
d
51
Subtyping
  • The type of an object is its class
  • The type associated with a subclass is treated as
    a subtype of the type assoc with superclass
  • Example
  • class A() ...
  • A class B() ...
  • ref (A) a - new A()
  • ref (B) b - new B()
  • a b / legal since B is subclass of A
    /
  • ...
  • b a / also legal, but run-time test
    /

52
Main object-oriented features
  • Classes
  • Objects
  • Inheritance (class prefixing)
  • Subtyping
  • Virtual methods
  • A function can be redefined in subclass
  • Inner
  • Combines code of superclass with code of subclass
  • Inspect/Qua
  • run-time class/type tests

53
Features absent from Simula 67
  • Encapsulation
  • All data and functions accessible no private,
    protected
  • Self/Super mechanism of Smalltalk
  • But has an expression this?class? to refer to
    object itself, regarded as object of type
    ?class?. Not clear how powerful this is
  • Class variables
  • But can have global variables
  • Exceptions
  • Not an OO feature anyway ...

54
Simula Summary
  • Class
  • procedure" that returns ptr to activation record
  • initialization code always run as procedure body
  • Objects closure created by a class
  • Encapsulation
  • protected and private not recognized in 1967
  • added later and used as basis for C
  • Subtyping determined by class hierarchy
  • Inheritance provided by class prefixing
Write a Comment
User Comments (0)
About PowerShow.com