8/10/2019 Oop Rosen Schein
1/56
Object Oriented Programming:Its Origins and Importance in
Computer Science
Prof. Jeffrey S. Rosenschein
School of Computer Science and Engineering
Hebrew University, Jerusalem, Israel
8/10/2019 Oop Rosen Schein
2/56
2
Computer Programming
The history of computerprogramming is a steady move
away from machine-oriented
views of programming towardsconcepts and metaphors that
more closely reflect the way in
which we ourselves understand
the world
8/10/2019 Oop Rosen Schein
3/56
3
Programming progression
Programming has progressed through:
machine code
assembly language machine-independent programming
languages
procedures & functions
objects
8/10/2019 Oop Rosen Schein
4/56
4
Machine languageMark I
8/10/2019 Oop Rosen Schein
5/56
5
Machine Language
0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
8/10/2019 Oop Rosen Schein
6/56
6
Assembly LanguagePDP-11
8/10/2019 Oop Rosen Schein
7/56
7
Assembly LanguageMacro-11
GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4DIV B, R4
MOV B, A
MOV R5, BCALL GCD
SIMPLE: RETURN
8/10/2019 Oop Rosen Schein
8/56
8
Assembly LanguageMacro-11
GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4DIV B, R4
MOV B, A
MOV R5, BCALL GCD
SIMPLE: RETURN
8/10/2019 Oop Rosen Schein
9/56
9
Machine-Independent Programming Languages
Fortran ! This example program solves for roots of the quadratic equation,! ax^2 +bx +c =0,for given values of a, b and c.
!PROGRAM bisection
IMPLICIT NONE
INTEGER :: iteration
DOUBLE PRECISION :: CC, Er, xl, x0, x0_old, xr
! Set convergence criterion and guess for xl, xr.
CC = 1.d-4
xl = 8.d-1xr = 11.d-1
! Bisection method.
Er =CC +1
iteration = 0
DO WHILE (Er > CC)
iteration = iteration + 1
! Compute x0 and the error.
x0_old = x0
x0 = (xl + xr) / 2.d0
Er = DABS((x0 - x0_old)/x0)*100.d0
WRITE (*,10) iteration, x0_old, x0, Er
10 FORMAT (1X,I4,3(2X,E10.4)) this is partial
8/10/2019 Oop Rosen Schein
10/56
10
Procedures & FunctionsPascal
program ValueArg(output);
{Shows how to arrange for a procedure to have arguments.}
procedure PrintInitials(First, Last : char);
{Within this procedure, the names First and Last represent theargument values. Well call write to print them.}
begin
write(My initials are: );
write(First);
writeln(Last)
end; {PrintInitials}
beginPrintInitials (D, C); {Any two characters can be arguments.}
PrintInitials (Q, T); {Like strings, characters are quoted.}
PrintInitials (&, #)
end. {ValueArg}
8/10/2019 Oop Rosen Schein
11/56
11
Objects
(My examples will be from Java)classTime {
private inthour, minute;
publicTime (inth, intm) {
hour = h;minute = m;}
publicvoid addMinutes (intm) {inttotalMinutes =
((60*hour) + minute + m) % (24*60);if(totalMinutes
8/10/2019 Oop Rosen Schein
12/56
12
Intrinsic Power vs. Effective Power
This progression is nota matter of intrinsic power
Anything you can do with a minimally capable
computer language, you can theoretically do with
any other minimally capable computer language
But that is like saying a shovel is theoretically ascapable as a tractor. In practice, using a shovel
might make things very hard
8/10/2019 Oop Rosen Schein
13/56
13
Objects
hour
minute
void addMinutes( int m )
TimeinTimeAttributes:
hour = 8minute = 30
Methods:void addMinutes(int m)
outTimeAttributes:
hour = 17
minute = 35Methods:
void addMinutes(int m)
class
objects
8/10/2019 Oop Rosen Schein
14/56
14
Classes and Objects
A classis a prototype for creatingobjects
When we write a program in an
object-oriented language like Java,we define classes, which in turn are
used to create objects
A class has a constructorforcreating objects
8/10/2019 Oop Rosen Schein
15/56
15
classTime {
private inthour, minute;
publicTime (inth, intm) {hour = h;minute = m;
}
publicvoid addMinutes (intm) {inttotalMinutes =
((60*hour) + minute + m) % (24*60);if(totalMinutes
8/10/2019 Oop Rosen Schein
16/56
16
Definition of an Object
An object is a computational entity that:
1. Encapsulatessome state
2. Is able to perform actions, or methods, on this
state3. Communicates with other objects via message
passing
8/10/2019 Oop Rosen Schein
17/56
17
1)Encapsulatessome state
Like a record in Pascal, it has a set ofvariables (of possibly different types)
that describe an objects state
These variables are sometimes calledan objects attributes(or fields, or
instance variables, or datamembers,
or )
8/10/2019 Oop Rosen Schein
18/56
18
Pascal Example: Represent a Time
type TimeTYPE = record
Hour: 1..23;
Minute: 0..59;end;
var inToWork, outFromWork: TimeTYPE;
8/10/2019 Oop Rosen Schein
19/56
19
Java Example: Represent a TimeclassTime {
private inthour, minute;
publicTime (inth, intm) {
hour = h;minute = m;}
}
Time inToWork = newTime 8, 30);
Time outFromWork = newTime 17, 35);
constructorfor Time
attributesof Time
8/10/2019 Oop Rosen Schein
20/56
20
Objects
hour
minute
void addMinutes( int m )
TimeinToWork
Attributes:hour = 8minute = 30
Methods:void addMinutes(int m)
outFromWork
Attributes:
hour = 17minute = 35Methods:
void addMinutes(int m)
class
objects
8/10/2019 Oop Rosen Schein
21/56
21
classTime {
private inthour, minute;
publicTime (inth, intm) {hour = h;minute = m;
}
publicvoid addMinutes (intm) {inttotalMinutes =
((60*hour) + minute + m) % (24*60);if(totalMinutes
8/10/2019 Oop Rosen Schein
22/56
22
3) Communicates with other objects via
message passing
Sends messages to objects, triggering
methods in those objects
void
8/10/2019 Oop Rosen Schein
23/56
23
Example of Object Creation
and Message Passing
bill
Attributes:
Methods:
8/10/2019 Oop Rosen Schein
24/56
24
Example of Object Creation
and Message Passing
bill
Attributes:
Methods:
In one of bills methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
8/10/2019 Oop Rosen Schein
25/56
25
Example of Object Creation
and Message Passing
inToWorkAttributes:hour = 8minute = 30
Methods:void addMinutes(int m)
bill
Attributes:
Methods:
In one of bills methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
8/10/2019 Oop Rosen Schein
26/56
26
Example of Object Creation
and Message Passing
inToWorkAttributes:hour = 8minute = 30
Methods:void addMinutes(int m)
inToWork.addMinutes(15)
bill
Attributes:
Methods:
In one of bills methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
8/10/2019 Oop Rosen Schein
27/56
27
Example of Object Creation
and Message Passing
inToWorkAttributes:hour = 8minute = 45
Methods:void addMinutes(int m)
bill
Attributes:
Methods:
In one of bills methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
8/10/2019 Oop Rosen Schein
28/56
28
Structure of a Class Definition
class
name{
declarations
constructor definition(s)
method definitions
}
attributes andsymbolic constants
how to create andinitialize objects
how to manipulatethe state of objects
These parts of a class can
actually be in any order
8/10/2019 Oop Rosen Schein
29/56
29
History of Object-Oriented Programming
Started out for simulation of complex man-machine systems, but was soon realized thatit was suitable for all complex programmingprojects
SIMULA I (1962-65) and Simula 67 (1967)were the first two object-oriented languages Developed at the Norwegian Computing Center,
Oslo, Norway by Ole-Johan Dahl and Kristen
Nygaard Simula 67 introduced most of the key concepts of
object-oriented programming: objects and classes,subclasses (inheritance), virtual procedures
8/10/2019 Oop Rosen Schein
30/56
30
The Ideas Spread
Alan Kay, Adele Goldberg and colleagues atXerox PARC extend the ideas of Simula indeveloping Smalltalk (1970s) Kay coins the term object oriented
Smalltalk is first fully object oriented language Grasps that this is a new programming paradigm
Integration of graphical user interfaces andinteractive program execution
Bjarne Stroustrup develops C++ (1980s) Brings object oriented concepts into the C
programming language
8/10/2019 Oop Rosen Schein
31/56
31
Other Object Oriented Languages
Eiffel (B. Meyer) CLOS (D. Bobrow, G. Kiczales)
SELF (D. Ungar et al.)
Java (J. Gosling et al.) BETA (B. Bruun-Kristensen, O. Lehrmann
Madsen, B. Mller-Pedersen, K. Nygaard)
Other languages add object dialects, such asTurboPascal
8/10/2019 Oop Rosen Schein
32/56
32
REVIEW:Definition of an Object
An object is a computational entity that:
Encapsulatessome state
Is able to perform actions, or methods, on this
state Communicates with other objects via message
passing
8/10/2019 Oop Rosen Schein
33/56
33
Encapsulation The main point is that by thinking of the
system as composed of independent objects,we keep sub-parts reallyindependent
They communicate only through well-defined
message passing Different groups of programmers can work on
different parts of the project, just making sure
they comply with an interface
It is possible to build larger systems with less
effort
8/10/2019 Oop Rosen Schein
34/56
34
Advantages
Building the system as a group of interactingobjects:
Allows extreme modularity between
pieces of the system May better match the way we (humans)
think about the problem
Avoids recoding, increases code-reuse
8/10/2019 Oop Rosen Schein
35/56
35
But theres more
Classes can be arranged in a hierarchy
Subclasses inheritattributes and
methods from their parent classes
This allows us to organize classes, andto avoid rewriting codenew classes
extendold classes, with little extra work!
Allows for large, structured definitions
8/10/2019 Oop Rosen Schein
36/56
36
Example of Class Inheritance
extends
extends
toString( )equals( Object obj )getClass( )
Shape
Rectangle
Time
extends
extends
Objects made from this
class, for example, have
allthe attributesand
methodsof the classes
above them, all the way
up the tree
Object
Circle Triangleextends
void addMinutes( int m )
hourminute
colorborderWidth
Color getColor( )void setBorderWidth( int m )
int computeArea( )
lengthwidth
int computeArea( )
radius baseheight
int computeArea( )
8/10/2019 Oop Rosen Schein
37/56
37
Java Class Hierarchy
8/10/2019 Oop Rosen Schein
38/56
38
Java Class Hierarchy, another view
8/10/2019 Oop Rosen Schein
39/56
39
Polymorphism An object has multiple identities, based on
its class inheritance tree It can be used in different ways
8/10/2019 Oop Rosen Schein
40/56
40
Polymorphism An object has multiple identities, based on
its class inheritance tree It can be used in different ways
A Circle is-a Shape is-a Object
toString( )equals( Object obj )getClass( )
Shapeextends
extends
Object
Circle
colorborderWidth
Color getColor( )void setBorderWidth( int m )
int computeArea( )
radius
8/10/2019 Oop Rosen Schein
41/56
41
Polymorphism An object has multiple identities, based on
its class inheritance tree It can be used in different ways
A Circle is-a Shape is-a Object
toString( )equals( Object obj )getClass( )
Shapeextends
extends
Object
Circle
colorborderWidth
Color getColor( )void setBorderWidth( int m )
int computeArea( )
radius
Shape
Circle
Object
A Circle object really has 3 parts
8/10/2019 Oop Rosen Schein
42/56
42
How Objects are CreatedCircle c = newCircle );
8/10/2019 Oop Rosen Schein
43/56
43
How Objects are CreatedCircle c = newCircle );
c
Shape
Circle
Object
1.
Execution Time
8/10/2019 Oop Rosen Schein
44/56
44
How Objects are CreatedCircle c = newCircle );
c
Shape
Circle
Object
c
Shape
Circle
Object
1. 2.
Execution Time
8/10/2019 Oop Rosen Schein
45/56
45
How Objects are CreatedCircle c = newCircle );
c
Shape
Circle
Object
c
Shape
Circle
Object
c
Shape
Circle
Object
1. 2. 3.
Execution Time
8/10/2019 Oop Rosen Schein
46/56
46
Three Common Uses for Polymorphism
1. Using Polymorphism in Arrays
2. Using Polymorphism for Method
Arguments3. Using Polymorphism for Method
Return Type
8/10/2019 Oop Rosen Schein
47/56
47
1) Using Polymorphism in Arrays
We can declare an array to be filled with Shape
objects, then put in Rectangles, Circles, orTriangles
extends
toString( )equals( Object obj )getClass( )
Shape
Rectangle
extends
extends
Object
Circle Triangleextends
colorborderWidth
Color getColor( )void setBorderWidth( int m )
int computeArea( )
lengthwidth
int computeArea( )
radius baseheight
int computeArea( )
8/10/2019 Oop Rosen Schein
48/56
48
1) Using Polymorphism in Arrays
We can declare an array to be filled with Shape
objects, then put in Rectangles, Circles, orTriangles
samples(an arrayof Shapeobjects)
[0] [1] [2]
8/10/2019 Oop Rosen Schein
49/56
49
1) Using Polymorphism in Arrays
We can declare an array to be filled with Shape
objects, then put in Rectangles, Circles, orTriangles
[0] [1] [2]
[2]
firstShape
Attributes:length = 17width = 35
Methods:int computeArea( )
secondShape
Attributes:radius = 11
Methods:int computeArea( )
thirdShape
Attributes:base = 15height = 7
Methods:int computeArea( )
samples(an arrayof Shapeobjects)
8/10/2019 Oop Rosen Schein
50/56
50
1) Using Polymorphism in Arrays
We can declare an array to be filled with Shape
objects, then put in Rectangles, Circles, orTriangles
[0] [1] [2]
[2]
firstShape
Attributes:length = 17width = 35
Methods:int computeArea( )
secondShape
Attributes:radius = 11
Methods:int computeArea( )
thirdShape
Attributes:base = 15height = 7
Methods:int computeArea( )
samples(an arrayof Shapeobjects)
8/10/2019 Oop Rosen Schein
51/56
51
2) Using Polymorphism for Method
Arguments
We can create a procedure that has Shape as
the type of its argument, then use it for objects
of type Rectangle, Circle, and Triangle
publicintcalculatePaint (ShapemyFigure) {
finalintPRICE = 5;
inttotalCost = PRICE * myFigure.computeArea( );return totalCost;
}
The actual definition of computeArea( ) is known only at
runtime, not compile timethis is dynamic binding
8/10/2019 Oop Rosen Schein
52/56
52
2) Using Polymorphism for Method
Arguments
Polymorphism give us a powerful way of
writing code that can handle multiple types
of objects, in a unified way
publicintcalculatePaint (ShapemyFigure) {
finalintPRICE = 5;
inttotalCost = PRICE * myFigure.computeArea( );return totalCost;
}
To do this, we need to declare in Shapes class definition
that its subclasses will define the method computeArea( )
8/10/2019 Oop Rosen Schein
53/56
53
3) Using Polymorphism for Method
Return Type
We can write general code, leaving the typeof object to be decided at runtime
publicShapecreatePicture ( ) {
/* Read in choice from user */System.out.println(1 for rectangle, +2 for circle, 3 for triangle:);
SimpleInput sp = new SimpleInput(System.in);
inti = sp.readInt( );if( i == 1 ) return newRectangle(17, 35);if( i == 2 ) return newCircle(11);if( i == 3 ) return newTriangle(15, 7);
}
8/10/2019 Oop Rosen Schein
54/56
54
Java Convinced Me to Start Teaching
Object-Oriented in Intro to CS
Java is Object-Oriented from the Ground Up
Java has the elegance that comes from being
designed afterother OO languages had been in use
for many years Java has strong type checking
Java handles its own memory allocation
Javas syntax is standard (similar to C and C++)
Java is a good teaching language, but it (or
something close) will also be seen by students in
industry
b d d
8/10/2019 Oop Rosen Schein
55/56
55
Object-Oriented Programming in Industry
Large projects are routinely programmed
using object-oriented languages
nowadays
MS-Windows and applications in MS-Officeall developed using object-
oriented languages
This is the world into which our studentsare graduating
8/10/2019 Oop Rosen Schein
56/56
Conclusions
Object-oriented programming provides a superior
way of organizing programming projects
It encourages a high degree of modularity in
programming, making large projects easier to
implement
It provides powerful techniques like inheritance and
polymorphism to help organize and reuse code
Object-oriented languages like Java both provide
a good environment for beginning students tolearn programming, and match real-world
developments in computer programming