Download Lecture 8: Object-Oriented Programming in ECS 140A - Prof. Zhendong Su and more Assignments Programming Languages in PDF only on Docsity!
Prof. Su ECS 140A Lecture 8 1
Programming Languages
ECS 140A
13:40-15:00 Tu/Th
55 Roessler
Prof. Su ECS 140A Lecture 8 2
Announcements/reminders
- HW2 due midnight April 23 rd
- HW3 assigned, due May 7th
- In-class midterm: May 12th
Prof. Su ECS 140A Lecture 8 3
Object-oriented programming
- Primary object-oriented language concepts
- Dynamic lookup
- Encapsulation
- Inheritance
- Subtyping
- Program organization
- Work queue
- Geometry program
- Design patterns
Prof. Su ECS 140A Lecture 8 4
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:
hidden data msg 1 method 1
...... msgn methodn
Prof. Su ECS 140A Lecture 8 5
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
Prof. Su ECS 140A Lecture 8 6
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
Prof. Su ECS 140A Lecture 8 10
Encapsulation
- Builder of a concept has detailed view
- User of a concept has โabstractโ view
- Encapsulation separates these two views
- Implementation code: operate on representation
- Client code: operate by applying fixed set of operations provided by implementer of abstraction
message Object
Prof. Su ECS 140A Lecture 8 11
Language concepts
- โDynamic lookupโ
- different code for different object
- integer โ+โ different from real โ+โ
- Encapsulation
- Implementer of a concept has detailed view
- User has โabstractโ view
- Encapsulation separates these two views
- Subtyping
- Inheritance
Prof. Su ECS 140A Lecture 8 12
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
Prof. Su ECS 140A Lecture 8 13
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
Prof. Su ECS 140A Lecture 8 14
Subtyping
- If interface A contains all of interface B, then A objects can also be used as B objects.
ColorPoint interface contains Point
- ColorPoint is a subtype of Point
Point
x-coord y-coord move
ColorPoint
x-coord y-coord color move change_color
Prof. Su ECS 140A Lecture 8 15
Inheritance
- Implementation mechanism
- New objects may be defined by reusing
implementations of other objects
Prof. Su ECS 140A Lecture 8 19
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
Prof. Su ECS 140A Lecture 8 20
Subtype hierarchy
Shape
Circle Rectangle
- General interface defined in the shape class
- Implementations defined in circle, rectangle
- Extend hierarchy with additional shapes
Prof. Su ECS 140A Lecture 8 21
Code placed in classes
- Dynamic lookup
- circle -> move(x,y) calls function c_move
- Conventional organization
- Place c_move, r_move in move function
center move rotate print Circle c_center c_move c_rotate c_print
Rectangle r_center r_move r_rotate r_print
Prof. Su ECS 140A Lecture 8 22
Example use: Processing Loop
Remove shape from work queue
Perform action
Control loop does not know the type of each shape
Prof. Su ECS 140A Lecture 8 23
Subtyping differs from inheritance
Collection
Set
Sorted Set
Indexed
Array (^) Dictionary
String (^) Subtyping Inheritance
Prof. Su ECS 140A Lecture 8 24
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
Prof. Su ECS 140A Lecture 8 28
Summary
- Object-oriented design
- Primary object-oriented language concepts
- Dynamic lookup
- Encapsulation
- Inheritance
- Subtyping
- Program organization
- Work queue, geometry program, design patterns
- Comparison