Object Oriented Programming:
Its Origins and Importance in
Computer Science
Prof. Jeffrey S. Rosenschein
School of Computer Science and Engineering
Hebrew University, Jerusalem, Israel
2
Computer Programming
 The history of computer
programming is a steady move
away from machine-oriented
views of programming towards
concepts and metaphors that
more closely reflect the way in
which we ourselves understand
the world
3
Programming progression…
 Programming has progressed through:
 machine code
 assembly language
 machine-independent programming
languages
 procedures & functions
 objects
4
Machine language – Mark I
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
6
Assembly Language – PDP-11
7
Assembly Language – Macro-11
GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4
DIV B, R4
MOV B, A
MOV R5, B
CALL GCD
SIMPLE: RETURN
8
Assembly Language – Macro-11
GCD: TST B
BEQ SIMPLE
MOV A, R5
SXT R4
DIV B, R4
MOV B, A
MOV R5, B
CALL GCD
SIMPLE: RETURN
20/20
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-1
xr = 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…
10
Procedures & Functions – Pascal
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 the
argument values. We’ll call write to print them.}
begin
write(‘My initials are: ’);
write(First);
writeln(Last)
end; {PrintInitials}
begin
PrintInitials (‘D’, ‘C’); {Any two characters can be arguments.}
PrintInitials (‘Q’, ‘T’); {Like strings, characters are quoted.}
PrintInitials (‘&’, ‘#’)
end. {ValueArg}
11
Objects
 (My examples will be from Java)
class Time {
private int hour, minute;
public Time (int h, int m) {
hour = h;
minute = m;
}
public void addMinutes (int m) {
int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}
this is partial…
12
“Intrinsic Power” vs. “Effective Power”
 This progression is not a 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 as
capable as a tractor. In practice, using a shovel
might make things very hard…
13
Objects
hour
minute
void addMinutes( int m )
Time
inTime
Attributes:
hour = 8
minute = 30
Methods:
void addMinutes(int m)
outTime
Attributes:
hour = 17
minute = 35
Methods:
void addMinutes(int m)
class
objects
14
Classes and Objects
 A class is a prototype for creating
objects
 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 constructor for
creating objects
15
class Time {
private int hour, minute;
public Time (int h, int m) {
hour = h;
minute = m;
}
public void addMinutes (int m) {
int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}
A Simple Class, called “Time” (partial)
constructor for Time
16
Definition of an “Object”
 An object is a computational entity that:
1. Encapsulates some state
2. Is able to perform actions, or methods, on this
state
3. Communicates with other objects via message
passing
17
1) Encapsulates some state
 Like a record in Pascal, it has a set of
variables (of possibly different types)
that describe an object’s state
 These variables are sometimes called
an object’s attributes (or fields, or
instance variables, or datamembers,
or …)
18
Pascal Example: Represent a Time
type TimeTYPE = record
Hour: 1..23;
Minute: 0..59;
end;
var inToWork, outFromWork: TimeTYPE;
19
Java Example: Represent a Time
class Time {
private int hour, minute;
public Time (int h, int m) {
hour = h;
minute = m;
}
…
}
Time inToWork = new Time(8, 30);
Time outFromWork = new Time(17, 35);
constructor for Time
attributes of Time
20
Objects
hour
minute
void addMinutes( int m )
Time
inToWork
Attributes:
hour = 8
minute = 30
Methods:
void addMinutes(int m)
outFromWork
Attributes:
hour = 17
minute = 35
Methods:
void addMinutes(int m)
class
objects
21
class Time {
private int hour, minute;
public Time (int h, int m) {
hour = h;
minute = m;
}
public void addMinutes (int m) {
int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}
2) Is able to perform actions, or methods,
on this state
 More than a Pascal record!
 An object can also include a group of procedures/functions
that carry out actions
a method of Time
22
3) Communicates with other objects via
message passing
 Sends messages to objects, triggering
methods in those objects
void
23
Example of Object Creation
and Message Passing
bill
Attributes:
…
Methods:
…
24
Example of Object Creation
and Message Passing
bill
Attributes:
…
Methods:
…
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
25
Example of Object Creation
and Message Passing
inToWork
Attributes:
hour = 8
minute = 30
Methods:
void addMinutes(int m)
bill
Attributes:
…
Methods:
…
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
26
Example of Object Creation
and Message Passing
inToWork
Attributes:
hour = 8
minute = 30
Methods:
void addMinutes(int m)
inToWork.addMinutes(15)
bill
Attributes:
…
Methods:
…
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
27
Example of Object Creation
and Message Passing
inToWork
Attributes:
hour = 8
minute = 45
Methods:
void addMinutes(int m)
bill
Attributes:
…
Methods:
…
In one of bill’s methods, the following code appears:
Time inToWork = new Time(8, 30);
inToWork.addMinutes(15);
28
Structure of a Class Definition
class name {
declarations
constructor definition(s)
method definitions
}
attributes and
symbolic constants
how to create and
initialize objects
how to manipulate
the state of objects
These parts of a class can
actually be in any order
29
History of Object-Oriented Programming
 Started out for simulation of complex man-
machine systems, but was soon realized that
it was suitable for all complex programming
projects
 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
30
The Ideas Spread
 Alan Kay, Adele Goldberg and colleagues at
Xerox PARC extend the ideas of Simula in
developing Smalltalk (1970’s)
 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 and
interactive program execution
 Bjarne Stroustrup develops C++ (1980’s)
 Brings object oriented concepts into the C
programming language
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. Møller-Pedersen, K. Nygaard)
 Other languages add object dialects, such as
TurboPascal
 …
32
REVIEW: Definition of an “Object”
 An object is a computational entity that:
 Encapsulates some state
 Is able to perform actions, or methods, on this
state
 Communicates with other objects via message
passing
33
Encapsulation
 The main point is that by thinking of the
system as composed of independent objects,
we keep sub-parts really independent
 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
34
Advantages
Building the system as a group of interacting
objects:
 Allows extreme modularity between
pieces of the system
 May better match the way we (humans)
think about the problem
 Avoids recoding, increases code-reuse
35
But there’s more…
 Classes can be arranged in a hierarchy
 Subclasses inherit attributes and
methods from their parent classes
 This allows us to organize classes, and
to avoid rewriting code – new classes
extend old classes, with little extra work!
 Allows for large, structured definitions
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
all the attributes and
methods of the classes
above them, all the way
up the tree
Object
Circle Triangle
extends
void addMinutes( int m )
hour
minute
color
borderWidth
Color getColor( )
void setBorderWidth( int m )
int computeArea( )
length
width
int computeArea( )
radius base
height
int computeArea( )
37
Java Class Hierarchy
38
Java Class Hierarchy, another view
39
Polymorphism
 An object has “multiple identities”, based on
its class inheritance tree
 It can be used in different ways
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( )
Shape
extends
extends
Object
Circle
color
borderWidth
Color getColor( )
void setBorderWidth( int m )
int computeArea( )
radius
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( )
Shape
extends
extends
Object
Circle
color
borderWidth
Color getColor( )
void setBorderWidth( int m )
int computeArea( )
radius
Shape
Circle
Object
A Circle object really has 3 parts
42
How Objects are Created
Circle c = new Circle( );
43
How Objects are Created
Circle c = new Circle( );
c
Shape
Circle
Object
1.1.1.1.
Execution Time
44
How Objects are Created
Circle c = new Circle( );
c
Shape
Circle
Object
c
Shape
Circle
Object
1.1.1.1. 2.2.2.2.
Execution Time
45
How Objects are Created
Circle c = new Circle( );
c
Shape
Circle
Object
c
Shape
Circle
Object
c
Shape
Circle
Object
1.1.1.1. 2.2.2.2. 3.3.3.3.
Execution Time
46
Three Common Uses for Polymorphism
1. Using Polymorphism in Arrays
2. Using Polymorphism for Method
Arguments
3. Using Polymorphism for Method
Return Type
47
1) Using Polymorphism in Arrays
 We can declare an array to be filled with “Shape”
objects, then put in Rectangles, Circles, or
Triangles
extends
toString( )
equals( Object obj )
getClass( )
Shape
Rectangle
extends
extends
Object
Circle Triangle
extends
color
borderWidth
Color getColor( )
void setBorderWidth( int m )
int computeArea( )
length
width
int computeArea( )
radius base
height
int computeArea( )
48
1) Using Polymorphism in Arrays
 We can declare an array to be filled with “Shape”
objects, then put in Rectangles, Circles, or
Triangles
samples
(an array
of Shape
objects)
[0] [1] [2]
49
1) Using Polymorphism in Arrays
 We can declare an array to be filled with “Shape”
objects, then put in Rectangles, Circles, or
Triangles
[0] [1] [2]
[2]
firstShape
Attributes:
length = 17
width = 35
Methods:
int
computeArea( )
secondShape
Attributes:
radius = 11
Methods:
int computeArea( )
thirdShape
Attributes:
base = 15
height = 7
Methods:
int computeArea( )
samples
(an array
of Shape
objects)
50
1) Using Polymorphism in Arrays
 We can declare an array to be filled with “Shape”
objects, then put in Rectangles, Circles, or
Triangles
[0] [1] [2]
[2]
firstShape
Attributes:
length = 17
width = 35
Methods:
int
computeArea( )
secondShape
Attributes:
radius = 11
Methods:
int computeArea( )
thirdShape
Attributes:
base = 15
height = 7
Methods:
int computeArea( )Rectangle
Circle
Trianglesamples
(an array
of Shape
objects)
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
public int calculatePaint (Shape myFigure) {
final int PRICE = 5;
int totalCost = PRICE * myFigure.computeArea( );
return totalCost;
}
The actual definition of computeArea( ) is known only at
runtime, not compile time – this is “dynamic binding”
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
public int calculatePaint (Shape myFigure) {
final int PRICE = 5;
int totalCost = PRICE * myFigure.computeArea( );
return totalCost;
}
To do this, we need to declare in Shape’s class definition
that its subclasses will define the method computeArea( )
53
3) Using Polymorphism for Method
Return Type
 We can write general code, leaving the type
of object to be decided at runtime
public Shape createPicture ( ) {
/* Read in choice from user */
System.out.println(“1 for rectangle, ” +
“2 for circle, 3 for triangle:”);
SimpleInput sp = new SimpleInput(System.in);
int i = sp.readInt( );
if ( i == 1 ) return new Rectangle(17, 35);
if ( i == 2 ) return new Circle(11);
if ( i == 3 ) return new Triangle(15, 7);
}
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 after other OO languages had been in use
for many years
 Java has strong type checking
 Java handles its own memory allocation
 Java’s 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
55
Object-Oriented Programming in Industry
 Large projects are routinely programmed
using object-oriented languages
nowadays
 MS-Windows and applications in MS-
Office – all developed using object-
oriented languages
 This is the world into which our students
are graduating…
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 to
learn programming, and match real-world
developments in computer programming

More Related Content

PDF
Functional Programming You Already Know
PDF
The Ring programming language version 1.3 book - Part 82 of 88
PDF
L Fu - Dao: a novel programming language for bioinformatics
PDF
Object Oriented Programming using C++ Part III
PDF
Lecture 3: Storage and Variables
PPTX
Deep Learning in Your Browser
PDF
Lezione03
PDF
The Ring programming language version 1.9 book - Part 100 of 210
Functional Programming You Already Know
The Ring programming language version 1.3 book - Part 82 of 88
L Fu - Dao: a novel programming language for bioinformatics
Object Oriented Programming using C++ Part III
Lecture 3: Storage and Variables
Deep Learning in Your Browser
Lezione03
The Ring programming language version 1.9 book - Part 100 of 210

What's hot (18)

ODP
James Jesus Bermas on Crash Course on Python
PPT
C++ classes tutorials
PPTX
SPF Getting Started - Console Program
PPTX
Java Foundations: Objects and Classes
PPTX
11. Java Objects and classes
PPTX
TensorFlow for IITians
PPTX
Python advance
PPTX
PDF
PKC&RSA
PPT
C++ Returning Objects
PPT
Deuce STM - CMP'09
PPTX
11. Objects and Classes
PDF
Learn basics of Clojure/script and Reagent
PDF
PyTorch for Deep Learning Practitioners
PDF
C# Summer course - Lecture 3
PPTX
Object Oriented Programming - Value Types & Reference Types
PDF
Python for data science by www.dmdiploma.com
PDF
Julia - Easier, Better, Faster, Stronger
James Jesus Bermas on Crash Course on Python
C++ classes tutorials
SPF Getting Started - Console Program
Java Foundations: Objects and Classes
11. Java Objects and classes
TensorFlow for IITians
Python advance
PKC&RSA
C++ Returning Objects
Deuce STM - CMP'09
11. Objects and Classes
Learn basics of Clojure/script and Reagent
PyTorch for Deep Learning Practitioners
C# Summer course - Lecture 3
Object Oriented Programming - Value Types & Reference Types
Python for data science by www.dmdiploma.com
Julia - Easier, Better, Faster, Stronger
Ad

Similar to Oop rosenschein (20)

PDF
Chapter20 class-example-program
PDF
E7
PPT
lecture10 introduction to Classes and Objects.ppt
PPT
Materi TIK Class dan Objects Data O-O-P.ppt
PPT
lecture10.ppt
PPT
lecture10.ppt
PPT
lecture10.ppt fir class ibect fir c++ fr opps
PDF
maXbox Blix the Programmer
PDF
C++ L08-Classes Part1
PDF
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
PPT
Introduction to python & its applications.ppt
PPT
Class & Objects in JAVA.ppt
PPTX
C++ Presentation
DOCX
Structure in c sharp
PPT
C463_02_python.ppt
PPT
kapil presentation.ppt
PDF
Data Structure and Algorithms (DSA) with Python
PPTX
Introduction to Data Structure and algorithm.pptx
PPT
cpphtp4_PPT_06_Classes and Data Abstraction.ppt
PPT
C463_02_python.ppt
Chapter20 class-example-program
E7
lecture10 introduction to Classes and Objects.ppt
Materi TIK Class dan Objects Data O-O-P.ppt
lecture10.ppt
lecture10.ppt
lecture10.ppt fir class ibect fir c++ fr opps
maXbox Blix the Programmer
C++ L08-Classes Part1
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Introduction to python & its applications.ppt
Class & Objects in JAVA.ppt
C++ Presentation
Structure in c sharp
C463_02_python.ppt
kapil presentation.ppt
Data Structure and Algorithms (DSA) with Python
Introduction to Data Structure and algorithm.pptx
cpphtp4_PPT_06_Classes and Data Abstraction.ppt
C463_02_python.ppt
Ad

Recently uploaded (20)

PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
HVAC Specification 2024 according to central public works department
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Trump Administration's workforce development strategy
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
20th Century Theater, Methods, History.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
B.Sc. DS Unit 2 Software Engineering.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Virtual and Augmented Reality in Current Scenario
LDMMIA Reiki Yoga Finals Review Spring Summer
My India Quiz Book_20210205121199924.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
What if we spent less time fighting change, and more time building what’s rig...
Chinmaya Tiranga quiz Grand Finale.pdf
Empowerment Technology for Senior High School Guide
HVAC Specification 2024 according to central public works department
Share_Module_2_Power_conflict_and_negotiation.pptx
Trump Administration's workforce development strategy
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
20th Century Theater, Methods, History.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf

Oop rosenschein

  • 1. Object Oriented Programming: Its Origins and Importance in Computer Science Prof. Jeffrey S. Rosenschein School of Computer Science and Engineering Hebrew University, Jerusalem, Israel
  • 2. 2 Computer Programming  The history of computer programming is a steady move away from machine-oriented views of programming towards concepts and metaphors that more closely reflect the way in which we ourselves understand the world
  • 3. 3 Programming progression…  Programming has progressed through:  machine code  assembly language  machine-independent programming languages  procedures & functions  objects
  • 5. 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
  • 7. 7 Assembly Language – Macro-11 GCD: TST B BEQ SIMPLE MOV A, R5 SXT R4 DIV B, R4 MOV B, A MOV R5, B CALL GCD SIMPLE: RETURN
  • 8. 8 Assembly Language – Macro-11 GCD: TST B BEQ SIMPLE MOV A, R5 SXT R4 DIV B, R4 MOV B, A MOV R5, B CALL GCD SIMPLE: RETURN 20/20
  • 9. 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-1 xr = 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…
  • 10. 10 Procedures & Functions – Pascal 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 the argument values. We’ll call write to print them.} begin write(‘My initials are: ’); write(First); writeln(Last) end; {PrintInitials} begin PrintInitials (‘D’, ‘C’); {Any two characters can be arguments.} PrintInitials (‘Q’, ‘T’); {Like strings, characters are quoted.} PrintInitials (‘&’, ‘#’) end. {ValueArg}
  • 11. 11 Objects  (My examples will be from Java) class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } public void addMinutes (int m) { int totalMinutes = ((60*hour) + minute + m) % (24*60); if (totalMinutes<0) totalMinutes = totalMinutes + (24*60); hour = totalMinutes / 60; minute = totalMinutes % 60; } } this is partial…
  • 12. 12 “Intrinsic Power” vs. “Effective Power”  This progression is not a 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 as capable as a tractor. In practice, using a shovel might make things very hard…
  • 13. 13 Objects hour minute void addMinutes( int m ) Time inTime Attributes: hour = 8 minute = 30 Methods: void addMinutes(int m) outTime Attributes: hour = 17 minute = 35 Methods: void addMinutes(int m) class objects
  • 14. 14 Classes and Objects  A class is a prototype for creating objects  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 constructor for creating objects
  • 15. 15 class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } public void addMinutes (int m) { int totalMinutes = ((60*hour) + minute + m) % (24*60); if (totalMinutes<0) totalMinutes = totalMinutes + (24*60); hour = totalMinutes / 60; minute = totalMinutes % 60; } } A Simple Class, called “Time” (partial) constructor for Time
  • 16. 16 Definition of an “Object”  An object is a computational entity that: 1. Encapsulates some state 2. Is able to perform actions, or methods, on this state 3. Communicates with other objects via message passing
  • 17. 17 1) Encapsulates some state  Like a record in Pascal, it has a set of variables (of possibly different types) that describe an object’s state  These variables are sometimes called an object’s attributes (or fields, or instance variables, or datamembers, or …)
  • 18. 18 Pascal Example: Represent a Time type TimeTYPE = record Hour: 1..23; Minute: 0..59; end; var inToWork, outFromWork: TimeTYPE;
  • 19. 19 Java Example: Represent a Time class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } … } Time inToWork = new Time(8, 30); Time outFromWork = new Time(17, 35); constructor for Time attributes of Time
  • 20. 20 Objects hour minute void addMinutes( int m ) Time inToWork Attributes: hour = 8 minute = 30 Methods: void addMinutes(int m) outFromWork Attributes: hour = 17 minute = 35 Methods: void addMinutes(int m) class objects
  • 21. 21 class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } public void addMinutes (int m) { int totalMinutes = ((60*hour) + minute + m) % (24*60); if (totalMinutes<0) totalMinutes = totalMinutes + (24*60); hour = totalMinutes / 60; minute = totalMinutes % 60; } } 2) Is able to perform actions, or methods, on this state  More than a Pascal record!  An object can also include a group of procedures/functions that carry out actions a method of Time
  • 22. 22 3) Communicates with other objects via message passing  Sends messages to objects, triggering methods in those objects void
  • 23. 23 Example of Object Creation and Message Passing bill Attributes: … Methods: …
  • 24. 24 Example of Object Creation and Message Passing bill Attributes: … Methods: … In one of bill’s methods, the following code appears: Time inToWork = new Time(8, 30); inToWork.addMinutes(15);
  • 25. 25 Example of Object Creation and Message Passing inToWork Attributes: hour = 8 minute = 30 Methods: void addMinutes(int m) bill Attributes: … Methods: … In one of bill’s methods, the following code appears: Time inToWork = new Time(8, 30); inToWork.addMinutes(15);
  • 26. 26 Example of Object Creation and Message Passing inToWork Attributes: hour = 8 minute = 30 Methods: void addMinutes(int m) inToWork.addMinutes(15) bill Attributes: … Methods: … In one of bill’s methods, the following code appears: Time inToWork = new Time(8, 30); inToWork.addMinutes(15);
  • 27. 27 Example of Object Creation and Message Passing inToWork Attributes: hour = 8 minute = 45 Methods: void addMinutes(int m) bill Attributes: … Methods: … In one of bill’s methods, the following code appears: Time inToWork = new Time(8, 30); inToWork.addMinutes(15);
  • 28. 28 Structure of a Class Definition class name { declarations constructor definition(s) method definitions } attributes and symbolic constants how to create and initialize objects how to manipulate the state of objects These parts of a class can actually be in any order
  • 29. 29 History of Object-Oriented Programming  Started out for simulation of complex man- machine systems, but was soon realized that it was suitable for all complex programming projects  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
  • 30. 30 The Ideas Spread  Alan Kay, Adele Goldberg and colleagues at Xerox PARC extend the ideas of Simula in developing Smalltalk (1970’s)  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 and interactive program execution  Bjarne Stroustrup develops C++ (1980’s)  Brings object oriented concepts into the C programming language
  • 31. 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. Møller-Pedersen, K. Nygaard)  Other languages add object dialects, such as TurboPascal  …
  • 32. 32 REVIEW: Definition of an “Object”  An object is a computational entity that:  Encapsulates some state  Is able to perform actions, or methods, on this state  Communicates with other objects via message passing
  • 33. 33 Encapsulation  The main point is that by thinking of the system as composed of independent objects, we keep sub-parts really independent  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
  • 34. 34 Advantages Building the system as a group of interacting objects:  Allows extreme modularity between pieces of the system  May better match the way we (humans) think about the problem  Avoids recoding, increases code-reuse
  • 35. 35 But there’s more…  Classes can be arranged in a hierarchy  Subclasses inherit attributes and methods from their parent classes  This allows us to organize classes, and to avoid rewriting code – new classes extend old classes, with little extra work!  Allows for large, structured definitions
  • 36. 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 all the attributes and methods of the classes above them, all the way up the tree Object Circle Triangle extends void addMinutes( int m ) hour minute color borderWidth Color getColor( ) void setBorderWidth( int m ) int computeArea( ) length width int computeArea( ) radius base height int computeArea( )
  • 38. 38 Java Class Hierarchy, another view
  • 39. 39 Polymorphism  An object has “multiple identities”, based on its class inheritance tree  It can be used in different ways
  • 40. 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( ) Shape extends extends Object Circle color borderWidth Color getColor( ) void setBorderWidth( int m ) int computeArea( ) radius
  • 41. 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( ) Shape extends extends Object Circle color borderWidth Color getColor( ) void setBorderWidth( int m ) int computeArea( ) radius Shape Circle Object A Circle object really has 3 parts
  • 42. 42 How Objects are Created Circle c = new Circle( );
  • 43. 43 How Objects are Created Circle c = new Circle( ); c Shape Circle Object 1.1.1.1. Execution Time
  • 44. 44 How Objects are Created Circle c = new Circle( ); c Shape Circle Object c Shape Circle Object 1.1.1.1. 2.2.2.2. Execution Time
  • 45. 45 How Objects are Created Circle c = new Circle( ); c Shape Circle Object c Shape Circle Object c Shape Circle Object 1.1.1.1. 2.2.2.2. 3.3.3.3. Execution Time
  • 46. 46 Three Common Uses for Polymorphism 1. Using Polymorphism in Arrays 2. Using Polymorphism for Method Arguments 3. Using Polymorphism for Method Return Type
  • 47. 47 1) Using Polymorphism in Arrays  We can declare an array to be filled with “Shape” objects, then put in Rectangles, Circles, or Triangles extends toString( ) equals( Object obj ) getClass( ) Shape Rectangle extends extends Object Circle Triangle extends color borderWidth Color getColor( ) void setBorderWidth( int m ) int computeArea( ) length width int computeArea( ) radius base height int computeArea( )
  • 48. 48 1) Using Polymorphism in Arrays  We can declare an array to be filled with “Shape” objects, then put in Rectangles, Circles, or Triangles samples (an array of Shape objects) [0] [1] [2]
  • 49. 49 1) Using Polymorphism in Arrays  We can declare an array to be filled with “Shape” objects, then put in Rectangles, Circles, or Triangles [0] [1] [2] [2] firstShape Attributes: length = 17 width = 35 Methods: int computeArea( ) secondShape Attributes: radius = 11 Methods: int computeArea( ) thirdShape Attributes: base = 15 height = 7 Methods: int computeArea( ) samples (an array of Shape objects)
  • 50. 50 1) Using Polymorphism in Arrays  We can declare an array to be filled with “Shape” objects, then put in Rectangles, Circles, or Triangles [0] [1] [2] [2] firstShape Attributes: length = 17 width = 35 Methods: int computeArea( ) secondShape Attributes: radius = 11 Methods: int computeArea( ) thirdShape Attributes: base = 15 height = 7 Methods: int computeArea( )Rectangle Circle Trianglesamples (an array of Shape objects)
  • 51. 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 public int calculatePaint (Shape myFigure) { final int PRICE = 5; int totalCost = PRICE * myFigure.computeArea( ); return totalCost; } The actual definition of computeArea( ) is known only at runtime, not compile time – this is “dynamic binding”
  • 52. 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 public int calculatePaint (Shape myFigure) { final int PRICE = 5; int totalCost = PRICE * myFigure.computeArea( ); return totalCost; } To do this, we need to declare in Shape’s class definition that its subclasses will define the method computeArea( )
  • 53. 53 3) Using Polymorphism for Method Return Type  We can write general code, leaving the type of object to be decided at runtime public Shape createPicture ( ) { /* Read in choice from user */ System.out.println(“1 for rectangle, ” + “2 for circle, 3 for triangle:”); SimpleInput sp = new SimpleInput(System.in); int i = sp.readInt( ); if ( i == 1 ) return new Rectangle(17, 35); if ( i == 2 ) return new Circle(11); if ( i == 3 ) return new Triangle(15, 7); }
  • 54. 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 after other OO languages had been in use for many years  Java has strong type checking  Java handles its own memory allocation  Java’s 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
  • 55. 55 Object-Oriented Programming in Industry  Large projects are routinely programmed using object-oriented languages nowadays  MS-Windows and applications in MS- Office – all developed using object- oriented languages  This is the world into which our students are graduating…
  • 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 to learn programming, and match real-world developments in computer programming