principles of object oriented programmingoosd162/wiki.files/lintroductionse.pdf18 a new paradigm...

45
Principles of Object Oriented Programming עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן- גוריון ד מבוסס חלקית על השקפים של" ר גרא וייס

Upload: others

Post on 08-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Principles of Object Oriented

Programming

מרעיעזאםהמחלקה למדעי המחשב

גוריון-אוניברסיטת בן

ר גרא וייס"מבוסס חלקית על השקפים של ד

Page 2: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

General Information2

Instructor: Azzam Maraee

Office: building 58, room -106

Email: [email protected]

Course WEB site:

https://www.cs.bgu.ac.il/~poop162/

Page 3: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Book and credit for the slides3

We will mostly follow:

The slides are based on Timothy Budds’s slides for the book

Page 4: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Grading

4

Home work:

Programming exercises

40% of the total grade

Via the submission system

In class exam:

Theoretical problems

60% of the total grade

Pass condition for the course: Passing the final test.

Page 5: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Class Materials

1. Introduction to Object Oriented Programming

2. Abstraction

3. Object Oriented Design

4. Classes and Methods

5. Messages, Instances and Initialization

6. Inheritance and Substitution

7. Subclasses and Subtypes, Static and Dynamic Behavior

8. Implications of Substitution

9. Multiple Inheritance

10. Overloading and Overriding

11. Generics , Examples

12. Object Interconnections (Coupling and Cohesion)

5

Page 6: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Roadmap6

In this chapter we begin our exploration of

object-oriented programming.

Among the topics we will explore:

Historical survey of computing and programming

What is a paradigm ?

Why is this term used to describe the object-oriented

approach to problem solving?

How does language influence thought ?

Characteristics of the object-oriented way of thinking

Page 7: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Brief Historical Survey

2400 BC - Abacus (counting frame).

300 BC - The algorithm of Euclid for computing GCD(x,y)

100 BC - Antikythera mechanism.

800 - al-Khwārizmī systematic approach

to solving equations led to algebra.

Page 8: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Brief Historical Survey (cont.)

1842 – Ada Lovelace writes programs

for Babbage’s analytical engine.

1946 - The first electronic computer.

1960’s – The software crisis

1977 - The United States Department of

Defense define Steelman language requirements that

lead to development of Ada programming language.

1990’s - Object Oriented Programming becomes dominant

programming methodology.

Page 9: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

What is software?

Computer programs and associated documentation

Software products may be developed for a particular customer or may be developed for a general market

Software products may be Generic - developed to be sold to a range of different customers Bespoke (custom) - developed for a single customer according to their

specification

Page 10: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

10

Page 11: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

What is the “software crisis”?

Major projects are meaningfully late.

Software costs more than predicted.

Software is unreliable.

Software is difficult to maintain.

Poor performance.

While hardware costs were decreasing, software costs were rising.

requires techniques to control the

complexity of large software systems.

Page 12: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Object Oriented Paradigm

12

Page 13: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Conflicting Objectives13

Along the way, we'll try to convince you the validity of the

following two assertions:

OOP is a revolutionary idea, totally unlike anything that

has come before in programming languages

OOP is an evolutionary step, following naturally on the

heels of earlier programming abstractions

Page 14: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

14

Why has OOP remained

popular for so long?

OOP has been the dominant programming paradigm for more

than twenty years. Why is it so popular?

Proven record of success

Scales well from small problems to large

Nevertheless,

programming is still a task that requires skill and learning

Page 15: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

BUT: The Software Werewolf

Software is like a werewolf—it looks normal until the

moon comes out and it turns into a monster

Missed deadlines

Blown budgets

Buggy software

We want the silver bullet to kill the

monster

something to make software costs drop as

rapidly as computer hardware costs do.

Frederick P. Brooks:No Silver Bullaet: Essence and Accidents of Software Engineering

Page 16: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

There Is No Silver Bullet!

Frederick P. Brooks: “As we look to the horizon of a decade hence, we see no

silver bullet.

There is no single development, in either

essence, technology or in management technique,

that by itself promises even one order-of-magnitude improvement in

productivity, in reliability, in simplicity”

Frederick P. Brooks, No Silver Bullet: Essence and Accidents of

Software Engineering

Page 17: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

17

Page 18: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

18

A New Paradigm

Object-oriented programming is often described as a new paradigm.

We start by considering the definition of this term:

Typical example or pattern of something; a model

There is a new paradigm for public art in this country

A worldview underlying the theories and methodology of a particular scientific subject

The discovery of universal gravitation became the paradigm of successful science

A set of linguistic items that form mutually exclusive choices in particular syntactic roles

English determiners form a paradigm: we can say “a book” or “his book” but not “a his book.”

A table of all the inflected forms of a particular verb, noun, or adjective, serving as a model for other words of the same conjugation or declension

What does this have to do with computer programming languages?

Page 19: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Programming Paradigm

“A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized”. Robert W. Floyd

Imperative : Machine-model based

Functional : Equations; Expression Evaluation

Logical : First-order Logic Deduction

Object-Oriented : Programming with Data Types

19

Page 20: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

20

Sapir-Whorf Hypothesis

language defines the way a person behaves and thinks:

Eskimo (or Inuit) languages and snow

George Orwell's book 1984: a language entitled "newspeak" created to change the way people think about the government

What is true of natural languages is even more true of artificial computer languages

Language, Thought, and RealitySelected Writings of Benjamin Lee Whorf

Page 21: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

21 Example from

Computer Languages

A student working in DNA research had the task of finding repeated sequences of M values in a long sequence of values:

ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA…

Wrote the simplest (and therefore, most efficient?) program:

DO 10 I = 1, N-M

DO 10 J = I+1, N-M

FOUND = .TRUE.

DO 20 K = 1, M

20 IF X[I+K-1] .NE. X[J+K-1] THEN FOUND = .FALSE.

IF FOUND THEN ...

10 CONTINUE

Took a depressingly long time.

Page 22: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

22

A Better Solution

A friend writing in APL found a much better solution by rearranging the data and sorting:

A C T C G G positions 1 to M

C T C G G A positions 2 to M+1

T C G G A T positions 3 to M+2

C G G A T T positions 4 to M+3

G G A T T C positions 5 to M+4

G A T T C T positions 6 to M+5

. . .

T G G A C C

G G A C C C

. . .

Ran surprisingly quickly, thesis saved

Page 23: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

23

What lead to the discovery?

Why did the APL programmer find the better

solution?

Fortran programmer was blinded by a culture that

valued loops, simple programs

Sorting is a built-in operation in APL, good

programmers try to find novel uses for sorting

The fundamental point is that the language you

speak leads you in one direction or another

Page 24: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

24

Church's Conjecture

In computer science we have the following assertion:

Anything can be done in any language, but it may simply be easier or

more efficient to use one language or another

Would you want to write an event-driven GUI interface in Turing

machine?

Bottom line: Languages lead you, but do not prevent you from going

anywhere you want

Church's Conjecture:

Any computation for which there exists an effective procedure can

be realized by a Turing machine

Page 25: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

25

Imperative Programming

Imperative programming is the “traditional” model of computation:

State / Variables / Assignment / Loops

A processing unit is separate from memory, and “acts” upon memory:

The computer is the data manager

Wandering through memory, pulling values in memory slots

Transforming them in some manner, and pushing results back in some other slots

Although this is exactly what happens inside a computer, it is not the way people do for solving problems

1. Programs written in imperative programming languages

consist of

1. A program state

2. Instructions that change the program state

2. Program instructions are “imperative” in the grammatical

sense of imperative verbs that express a command

3. Commands in an imperative language are similar to the

native machine instructions of traditional computer

hardware

Page 26: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

Flowchart

• Used to model imperative programs

• Based on the threecontrol statementsthat are essentialto have Turing machine capability

• Precursor of UML and other moderntechniques

• Originated to describe process flow in general

Page 27: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

28 Why Not Build a

Program out of Computers?

Alan Kay thought about this conventional

design of the computer, and asked:

Why we constructed the whole out of pieces

that were useless by themselves

Why not build a whole out of pieces that were

similar at all levels of detail? (think of fractals)

Idea:

A program can be build out of

little computing agents

Page 28: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

30 Kay's Description of Object-

Oriented Programming

Object-oriented programming is based on the principle of recursive design:

1. Everything is an object

2. Objects perform computation by making requests of each other through the passing of messages

3. Every object has it's own memory, which consists of other objects

4. Every object is an instance of a class. A class groups similar objects

5. The class is the repository for behavior associated with an object

6. Classes are organized into singly-rooted tree structure, called an inheritance hierarchy

We can illustrate these principles by considering how we go about solving a problem in real life

Page 29: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

31 Illustration of OOP Concepts --

Sending Flowers to a Friend

To illustrate the concepts of OOP in an easily understood framework, consider the problem of sending flowers to a friend who lives in a different city:

Chris is sending flowers to Robin.

Chris can't deliver them directly. So Chris uses the services of the local Florist.

Chris tells the Florist (named Fred) the address for Robin, how much to spend, and the type of flowers to send.

Fred contacts a florist in Robins city, who arranges the flowers, then contacts a driver, who delivers the flowers

If we start to think about it, there may even be other people involved in this transaction: There is the flower grower, perhaps somebody in charge of arrangements, and so on.

Page 30: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

32

Agents and Communities

Our first observation is that results are achieved through the

interaction of agents, which we will call objects

Furthermore, any nontrivial activity requires the interaction of an

entire community of objects working together

Each object has a part to play, a service they provide to the other

members of the community

Robin Delivery Person

Chris Fred

Robbin's Florist

Flower Arranger

Wholesaler

Grower

Gardeners

Page 31: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

33

Elements of OOP - Objects

Kay's first principle:

1) Everything is an object

Actions in OOP are performed by agents, called

instances or objects

There are many agents working together in our scenario

We have Chris, Robin, the florist, the florist in Robins city,

the driver, the flower arranger, and the grower

Each agent has a part to play, and the result is produced

when all work together in the solution of a problem

Page 32: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

34

Elements of OOP - Messages

And the second principle:

2) Objects perform computation by making requests of

each other through the passing of messages

Actions in OOP are produced in response to requests

for actions, called messages

Page 33: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

35

Elements of OOP - Messages

And the second principle:

2) Objects perform computation by making requests of each other through the passing of messages

Actions in OOP are produced in response to requests for actions, called messages

An instance may accept a message, and in return will perform an action and return a value

To begin the process of sending the flowers, Chrisgives a message to Fred. Fred in turn gives a message to the florist in Robins city, who gives another message to the driver, and so on

Robin Delivery Person

Chris Fred

Robbin's Florist

Flower Arranger

Wholesaler

Grower

Gardeners

Page 34: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

36

Information Hiding

Notice that the user of a service being provided by an

object, need only know the name of the messages that

the object will accept

The user need not know how the actions is performed

Having accepted a message, an object is responsible

for carrying it out

Page 35: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

37

Elements of OOP - Receivers

Messages differ from traditional function calls in two

very important respects:

In a message there is a designated receiver that

accepts the message

The interpretation of the message may be different,

depending upon the receiver

Page 36: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

39

Behavior and Interpretation

Although different objects may accept the same

message,

The actions (behavior) the object will perform will

likely be different

The determination of what behavior to perform may

be made at run-time, a form of late binding

The fact that the same name can mean two entirely

different operations is one form of polymorphism

Page 37: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

40 Elements of OOP –

Recursive Design

3) Every object has it's own memory, which consists

of other objects.

Each object is like a miniature computer itself - a

specialized processor performing a specific task

Page 38: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

42

“Ask not what you can do to your data structures,

but ask what your data structures can do for you”

-- Timothy A. Budd

Page 39: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

43

Elements of OOP - Classes

4) Every object is an instance of a class. A class groups similar objects.

5) The class is the repository for behavior associated with an object.

The behavior I expect from Fred is determined from a general idea I have of the behavior of Florists

We say Fred is an instance of the class Florist

Behavior is associated with classes, not with individual instances

All objects that are instances of a class use the same method in response to similar messages

Page 40: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

44

Hierarchies of Categories

But there is more that I know about Fred then just that he is a Florist

I know he is a Shopkeeper, and a Human, and a Mammal, and a

Material Objects, and so on

At each level of abstraction I have certain information recorded

That information is applicable to all lower (more specialized) levels

Material Object

Animal

Mammal

Human Shopkeeper Florist

Page 41: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

45

Class HierarchiesMaterial Object

Living Thing

Animal

Reptile Mammal

Human Being

Dentist

Ken

Shopkeeper

Flo

Artist

Beth

Cat Dog Platypus

Plant

Non-Living Thing

Rock

Air

Page 42: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

46

Elements of OOP - Inheritance

6) Classes are organized into a singly-rooted tree

structure, called an inheritance hierarchy

Information (data and/or behavior) associated with

one level of abstraction in a class hierarchy is

automatically applicable to lower levels of the

hierarchy

Page 43: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

47

Elements of OOP - Overriding

Subclasses can alter or override information inherited from parent classes:

All mammals give birth to live young

A platypus is an egg-laying mammal

Inheritance combined with overriding are where most of the power of OO originates

Page 44: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

49

Metaphor and Problem Solving

Because the OOP view is similar to the way in which people go about solving problems in real life (finding another agent to do the real work!),

intuition, ideas, and understanding from everyday experience can be brought to bear on computing

On the other hand, common sense was seldom useful when computers were viewed in the process-state model,

since few people solve their everyday problems using pigeon-holes

Page 45: Principles of Object Oriented Programmingoosd162/wiki.files/lintroductionSE.pdf18 A New Paradigm Object-oriented programming is often described as a new paradigm. We start by considering

51

Summary

Object-oriented programming is not simply features added to a programming language. Rather, it is a new way of thinking

Object-oriented programming views a program as acommunity of agents, termed objects

Each object is responsible for a specific task

An object is an encapsulation of state (data values) and behavior (operations)

The behavior of objects is dictated by the object’s class

An object will exhibit its behavior by invoking a method (similar to executing a procedure) in response to a message

Objects and classes extend the concept of abstract data types by adding the notion of inheritance