You are on page 1of 7

Object Oriented Programming (3rd course, group 2) 1.

Introduction: OOP Concepts

26.02.08

Programming Paradigms
OOP

Object Oriented Programming Concepts

Data Abstraction

Structured Programming

Imperative Programming

Structured Programming (I)

Structured Programming (II)


Operation abstraction
Structure of a module

Input values

Procedure

Interface Input data Output d t O t t data Functionality description

Implementation Local data Sequence of instructions S f i t ti

Main block

Input values

Function
Ouput value(s)

Language syntax
Organization of code in blocks of instructions Definition of functions and procedures Extension of the language with new operations Calls to new functions and procedures

Structured Programming: Benefits


Eases software development
Avoids repetition of work Programming work decomposed in independent modules Top-down design: decomposition into subproblems p g p p

Structured Programming: Example


void main () { double u1, u2, m; u1 = 4; u2 = -2; m = sqrt (u1*u1 + u2*u2); printf (%lf, m); }

Facilitates software maintenance


Easier to read code Independence of modules

double module (double u1, double u2) { double m; m = sqrt (u1*u1 + u2*u2); return m; } void main () { printf (%lf, module (4, -2)); }

Favors software reuse

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

Abstract Data Types


Data abstraction + operation abstraction
a b c

Data Abstraction
z x y

An Abstract Data Type consists of:


Data structure which stores information to represent a certain concept Functionality: set of operations that can be applied to the data type

Language syntax
Modules are associated to data types Not necessarily new syntax w.r.t. modular programming

Operation Abstraction
Entrada Entrada struct vector { double x; double y; };

TAD Example in C

arg1

arg1

arg2

arg3

Salida

Salida

void construct (vector *u, double u1, double u2) { u >x u->x = u1; u->y = u2; } void main () { double module (vector u) vector u; { construct (&u, 4, -2); double m; printf (%lf, module (u)); m = sqrt (u.x*u.x + u.y*u.y); } return m; }

10

TAD Extensibility
... double product (vector u, vector v) { return u.x * v.x + u.y * v.y; } void main () { vector u, v; construct (&u, 4, -2); construct (&v, 1, 5); printf (%lf, product (u, v)); }

Abstract Data Types: Benefits


Domain concepts are reflected in the code Encapsulation: internal complexity, data and operation details are hidden Specification vs. implementation: usage of data type is independent from its vs internal implementation Higher modularity: also data Increases ease of maintenance and reuse of code

11

12

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

Object Oriented Programming


Object Oriented Programming = Syntactic support for abstract data types + Facilities associated to class hierarchies + Change of perspective

Object Oriented Programming (I)


Explicit syntactic support for data abstraction Change of point of view: programs are appendices of data A new concept appears: object Object = abstract data type with state (attributes) and behavior (operations) The concept of class hierarchy is introduced, and with it:
Inheritance of structure and functionality Type polivalence Polimorfism

Functions Object

Language syntax:
Class definitions Functions explcitly associated to classes Creation of objects Access to attributes, method invocation
13 14

Variables

Object Oriented Programming (II)

Object Oriented Programming Example in Java


class Vector { private double x; private double y; Vector (double u1, double u2) { x = u1; y = u2; } double module () { return Math sqrt (x*x + y*y); } Math.sqrt (x x y y); } class MainClass { public static void main (String args []) { Vector u = new Vector (4, -2); System.out.println (u.module ()); } }

15

16

Object Oriented Programming Example in C++


class Vector { private: double x; double y; public: Vector (double u1, double u2) { x = u1; y = u2; } double module () { return sqrt (x*x + y*y); } }; void main () { Vector u (4, -2); cout << u.module (); }

Object Oriented Programming: Benefits


Benefits of data abstraction + programming methodology Code reuse and maintenance, extension of applications Component-oriented software development and i t C t i t d ft d l t d integration ti Language power: inheritance, polimorfism Reflect concepts of problem domains Easier to use?

17

18

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

OO Languages
Simula (1967) Smalltalk (1980) C++ (1983, 1990) Object Pascal (1988) Lisp CLOS (1989) Java (1995, 1997, 1998...) Pure vs. hybrid languages

OOP Elements
Objects: attributes + methods Methods: operations on objects g j properties and operations p Classes: categories of objects with common p p Class hierarchies In some languages classes are objects
Particular case: prototype-instance paradigm

Relations, composite objects

19

20

Conceptual Structure of an Object


Properties: values
Employee name birth division gross salary Employee Manager Manager name birth division gross salary category

Relations between Objects


Types of relations
Specialization: only between classes Aggregation: composite objects made of parts Association: arbitrary relations (e.g. supervisor of an employee)

Relations with other objects Methods: code


Access to object state Calculations over state Modification of state Constructors: initialization

Employee show personal data calculate net salary

Implementation
Employee

Specific language elements for relations Regular attributes that contain references (pointers) to objects

21

22

Objects and Encapsulation


Visible part: interface
Public contract of behavior Description of operations: input and output information

Object Life Cycle


Creation
Memory allocation: Employee x = create Employee () Initialization of attributes: constructors

Manipulation
Data

Hidden part: implementation


Data structure to store the information

Access to attributes: Method invocation:

x . name x . net_salary ( )

Destruction
Code that is executed to perform operations
Interface Methods
23

Free memory Destroy parts, if any Remove references to the destroyed object (e.g. supervisor)
24

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

Object Hierarchy
Person

Structure Inheritance
name birth Person

Type hierarchy
Person x Employee y = create Employee division gross salary

Employee l

Customer

Manager z = create Manager x=y x=z

Employee l

Customer

f p y name of company contact phone number

Manager

category

Manager

25

26

Functionality Inheritance
show personal data
specialization

Ease of Extension
Reuse
Person

Person

specialization

Modularity y
calculate net salary
inheritance

show personal data

Employee

Customer

show personal data show personal data

calculate net salary

Employee l

Customer

Administrative

Manager

calculate net salary

show personal data

Manager

27

28

Multiple Inheritance
year courses register (course) Student MSc year of graduation specialty qualifications y t

Ambiguity in Multiple Inheritance


x A

Graduate Student year of graduation specialty qualifications year courses register (course)

y z

D obj = create D obj . x obj . y

D x y z t
29 30

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

Polimorfism
Overloading
Line l1 = create Line Line l2 = create Line Vector v = create Vector l1.parallel (r2) l1.parallel (v)

Dynamic Linking
Calculate area of selected figure
area ( ) Figure g

Dynamic linking
Person x Employee y = create Employee x=y x.show_data x.show data ( ) y.show_data ( )

Overriding (specialization)
Person x = create Person Employee y = create Employee x.show_data ( ) y.show_data ( ) Triangle area ( ) Rectangle area ( ) Ellipse area ( )

31

32

Dynamic Linking of Arguments


Calculate area of the intersection of selected figures
Tringle double intersection (Triangle t) double intersection (Ellipse e) double intersection (Rectangle r) Rectangle double intersection (Triangle t) double intersection (Ellipse e) double intersection (Rectangle r) Ellipse double intersection (Triangle t) double intersection (Ellipse e) double intersection (Rectangle r)

Dynamic Linking of Arguments: Ambiguity (I)


A C f (A x, B y) f (B x, A y)

B B b1 = create B B b2 = create B C c = create C c . f (b1, b2)

33

34

Dynamic Linking of Arguments: Ambiguity (II)


A f (B x) B b = create B

Dynamic Linking of Arguments: Ambiguity (III)


A B D f (A x) f (B x)

?
B f (A x)

b . f (b)

?
C c = create C D d = create D

d . f (c)

35

36

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

Object Oriented Programming (3rd course, group 2) 1. Introduction: OOP Concepts

26.02.08

Class Diagram

OO Analysis and Design: Unified Modeling Language (UML)


Class Attributes Operations Object Attributes Operations Interface

Person name : String birth : Date 1..* Uses

Computer 0..* name : String memory : Integer

User1 : Person name = "John" John birth = 21/07/76

Serer7 : Computer name = "Andromeda" Andromeda memory = 256

Dependence Generalization Asociation Aggregation


37

Object Diagram

User2 : Person name = "Louis" age = 21/07/85

PC12 : Computer name = "Anubis" memory = 128

38

Formed by

Figure {abstract} * position : Point draw ( ) {abstract} area ( ) : Integer {abstract}

Formed by

Canvas

Group

Circle center : Point radio : Integer

Polygon Formed by * draw ( ) area ( ) : Integer

Point x : Integer y : Integer

draw ( ) area ( ) : Integer

draw ( ) area ( ) : Integer

39

Pablo Castells

Escuela Politcnica Superior Universidad Autnoma de Madrid

You might also like