Está en la página 1de 59

O B J E C T O R I E N T E D

P R O G R A M M I N G
Course 1
Loredana STANCIU
Lecturer at Automation and Applied Informatics Department of
Automation and Computers Faculty
loredana.stanciu@aut.upt.ro
www.aut.upt.ro/~loredanau
Room B616
ABOUT THE COURSE
Every week:
3 hours of course
Wednesday, 16:00-19:00 classroom A108
2 hours of practical activities:
Tuesday, 10:00-14:00, room B614
Thursday, 10:00-12:00, room B614
THE EXAMINATION
Distributed evaluation several marks
2 written examinations (multiple choice written
tests)
you have the possibility to improve your marks at written
tests
the average of these two marks = 2/3 of the final mark
2 practical tests
the marks from practical tests can be also improved
the average of these two marks = 1/3 of the final mark
SCHEDULER
W
1
W
2
W
3
W
4
W
5
W
6
W
7
W
8
W
9
W
1
0
W
1
1
W
1
2
W
1
3
W
1
4
C
o
u
r
s
C C C C C E1
P1
C
E1
P2
C C C E2
P1
E2
P2
L
a
b
L1
ET
L2
HW
L3
CO
L4
IOS
L5
I&P
L6
I
L7
Ex
L8
E
L9
T1
L10
Th
L11
G
L12
G
L13
T2
R
A SURVEY OF PROGRAMMING TECHNIQUES
Unstructured programming
UNSTRUCTURED PROGRAMMING
Small and simple programs
consisting only of one main
program
A sequence of commands
or statements which modify
data (global) throughout
the whole program
UNSTRUCTURED PROGRAMMING
#include <stdio.h>
#include <conio.h>
void main(void){
int a;
clrscr();
printf(a= ");scanf(%d,&a);
printf(a= %d,a);
}
UNSTRUCTURED PROGRAMMING
Disadvantage: in large programs using the
same statement sequence
Idea
to extract these sequences, name them and
offering a technique to call and return from
these procedures
A SURVEY OF PROGRAMMING TECHNIQUES
Unstructured programming
Procedural programming
PROCEDURAL PROGRAMMING
To combine returning
sequences of statements
into one single place
A procedure call is used to
invoke the procedure
With parameters and
subprocedures programs
can now be written more
structured and error free
PROCEDURAL PROGRAMMING
#include <stdio.h>
#include <conio.h>
int add(int a, int b){
return a+b;}
void main(void){
int a,b,c;
clrscr();
printf(a= ");scanf(%d,&a);
printf(b= ");scanf(%d,&b);
c=add(a,b);
printf(c= %d,c);
}
PROCEDURAL PROGRAMMING
A single program divided
into small pieces called
procedures
To enable usage of
general procedures or
groups of procedures in
other programs, they
must be separately
available
A SURVEY OF PROGRAMMING TECHNIQUES
Unstructured programming
Procedural programming
Modular programming
MODULAR PROGRAMMING
Procedures of a common
functionality are grouped
together into separate
modules
A program is now divided
into several smaller parts
which interact through
procedure calls and
which form the whole
program
EXAMPLE HANDLING SINGLE LISTS
Programs use data structures to store data
Characterized by their structure and their
access methods
To program a list in a modular programming
language:
the interface definition
the implementation file
EXAMPLE HANDLING SINGLE LISTS
/* * Interface definition for a module which implements
* a singly linked list for storing data of any type.
*/
MODULE Singly-Linked-List-1
BOOL list_initialize();
BOOL list_append(ANY data);
BOOL list_delete();
list_end();
ANY list_getFirst();
ANY list_getNext();
BOOL list_isEmpty();
END Singly-Linked-List-1
Hiding information
EXAMPLE HANDLING MULTIPLE LISTS
/* * A list module for more than one list. */
MODULE Singly-Linked-List-2
DECLARE TYPE list_handle_t;
list_handle_t list_create();
list_destroy(list_handle_t this);
BOOL list_append(list_handle_t this, ANY data);
ANY list_getFirst(list_handle_t this);
ANY list_getNext(list_handle_t this);
BOOL list_isEmpty(list_handle_t this);
END Singly-Linked-List-2;
List objects
MODULAR PROGRAMMING PROBLEMS
1) Explicit Creation and Destruction
PROCEDURE foo() BEGIN
list_handle_t myList;
myList <- list_create();
/* Do something with myList */
...
list_destroy(myList);
END
int x;
x=3;
MODULAR PROGRAMMING PROBLEMS
2) Decoupled Data and Operations
A structure based on the operations
rather than the data defined
operations specify the data to be used
In object-orientation, structure is
organized by the data modules group
data representations together
MODULAR PROGRAMMING PROBLEMS
3) Missing Type Safety
PROCEDURE foo() BEGIN
SomeDataType data1;
SomeOtherType data2;
list_handle_t myList;
myList <- list_create();
list_append(myList, data1);
list_append(myList, data2); /* Oops */ ...
list_destroy(myList);
END
MODULAR PROGRAMMING PROBLEMS
the compiler cannot guarantee for type
safety a mechanism which allows to
specify on which data type the list should
be defined
list_handle_t<Apple> list1; /* a list of apples */
list_handle_t<Car> list2; /* a list of cars */
MODULAR PROGRAMMING PROBLEMS
4) Strategies and Representation
a cursor is used to point to the current
element a traversing strategy which
defines the order in which the elements of
the data structure are to be visited
to separate the actual representation or
shape of the data structure from its
traversing strategy
A SURVEY OF PROGRAMMING TECHNIQUES
Unstructured programming
Procedural programming
Modular programming
Object-oriented programming
OBJECT-ORIENTED PROGRAMMING
solves some of the
mentioned
problems
a web of interacting
objects, each
house-keeping its
own state
OBJECT-ORIENTED PROGRAMMING
Each object implements its own module allowing
for example many lists to coexist
Each object is responsible to initialize and destroy
itself correctly
Some authors describe object-oriented
programming as programming abstract data types
and their relationships
Real-life problems are nebulous and the first thing
you have to do is to try to understand the problem
OBJECT-ORIENTED PROGRAMMING
To obtain ones own
abstract view, or
model, of the
problem
abstraction
HANDLING PROBLEMS
OBJECT-ORIENTED PROGRAMMING
To define properties of the problem:
the data which are affected and
the operations which are identified
An example: the administration of
employees in an institution:
name, date of birth, shape, social number, room
number, hair color, hobbies
problem specific properties data
the abstract employees operations
HANDLING PROBLEMS
OBJECT-ORIENTED PROGRAMMING
Abstraction is the structuring of a
nebulous problem into well-defined
entities by defining their data and
operations
Combine data and operations which are not
decoupled from each other
HANDLING PROBLEMS
OBJECT-ORIENTED PROGRAMMING
With abstraction one can create a well-
defined entity:
Define the data structure of a set of items
The data structure can only be accessed with
defined operations interface exported by
the entity
abstract data type (ADT)
PROPERTIES OF ABSTRACT DATA TYPES
OBJECT-ORIENTED PROGRAMMING
1) It exports a type
2) It exports a set of operations. This set is
called interface
3) Operations of the interface are the one
and only access mechanism to the type's
data structure
4) Axioms and preconditions define the
application domain of the type
PROPERTIES OF ABSTRACT DATA TYPES
OBJECT-ORIENTED PROGRAMMING
Encapsulation = The principle of hiding the
used data structure and to only provide a
well-defined interface
The separation of data structures and
operations and the constraint to only access
the data structure via a well-defined
interface allows you to choose data
structures appropriate for the application
environment
IMPORTANCE OF DATA STRUCTURE ENCAPSULATION
OBJECT-ORIENTED PROGRAMMING
Define an ADT for complex numbers
Structure (real part and imaginary part)
Operations (addition, subtraction, multiplication or
division)
Two ways of representing:
An array: x=c[0], y=c[1]
Two-valued record: x=c.x, y=c.y
EXAMPLE OF DATA STRUCTURE ENCAPSULATION
OBJECT-ORIENTED PROGRAMMING
A class:
an actual representation
of an Abstract Data Type
provides implementation
details for the data
structure used and
operations
IMPLEMENTATION OF ABSTRACT DATA TYPES
class Integer {
attributes:
int i
methods:
setValue(int n)
Integer addValue(Integer j)
}
Definition (Class) A class is the implementation of an abstract data type (ADT).
It defines attributes and methods which implement the data structure and
operations of the ADT, respectively.
OBJECT-ORIENTED PROGRAMMING
An object:
uniquely identifiable by a name
the set of values at a particular time is the
state of the object
Definition (Object) An object is an instance of a class. It can be uniquely
identified by its name and it defines a state which is represented by the values
of its attributes at a particular time
Definition (Behaviour) The behaviour of an object is defined by the set of
methods which can be applied on it.
OBJECT-ORIENTED PROGRAMMING
A running program is a pool of objects
where objects are created, destroyed and
interacting through messages
Integer i; /* Define a new integer object */
i.setValue(1); /* Set its value to 1 */
Definition (Message) A message is a request to an object to invoke one of
its methods. A message therefore contains:
the name of the method and
the arguments of the method.
Definition (Method) A method is associated with a class. An object invokes
a method as a reaction to receipt of a message.
OBJECT-ORIENTED PROGRAMMING
A program with interacting objects
OBJECT-ORIENTED PROGRAMMING
Procedural
programming
1. wait, until a key is
pressed.
2. get key value
3. write key value at
current cursor position
4. advance cursor
position
Object oriented
programming
1) the key receive a
message to change its
state to be pressed
2) its corresponding
object sends a message
to the screen object.
3) the screen object
receive the message to
display the associated
key value
Problem: how a character appears on the screen when you type a key
OBJECT-ORIENTED PROGRAMMING
Write a drawing program
various objects such as points, circles, rectangles,
triangles and many more
class Point {
attributes:
int x, y
methods:
setX(int newX)
getX()
setY(int newY)
getY() }
class Circle {
attributes:
int x, y, radius
methods:
setX(int newX)
getX()
setY(int newY)
getY()
setRadius(int newY)
getRadius() }
OBJECT-ORIENTED PROGRAMMING
Relationships
between two similar classes A-Kind-Of
relationship
Between objects of classes in A-Kind-Of
relationship Is-A relationship
OBJECT-ORIENTED PROGRAMMING
To build objects by combining them out of others
class Logo {
attributes:
Circle circle
Triangle triangle
methods:
set(Point where) }
Relationships: Part-Of relationship
OBJECT-ORIENTED PROGRAMMING
Inheritance
able to make use of the a-kind-of and is-a
relationship when the classes share properties
class Circle inherits from Point {
attributes:
int radius
methods:
setRadius(int newRadius)
getRadius() }
Circle acircle
acircle.setX(1) /* Inherited from Point */
acircle.setY(2)
acircle.setRadius(3) /* Added by Circle */
OBJECT-ORIENTED PROGRAMMING
Definition (Inheritance) Inheritance is the
mechanism which allows a class A to inherit
properties of a class B. We say A inherits from B.
Objects of class A thus have access to attributes and
methods of class B without the need to redefine
them.
Definition (Superclass/Subclass) If class A inherits
from class B, then B is called superclass of A. A is
called subclass of B
Superclasses are also called parent classes
Subclasses may also be called child classes or just
derived classes
OBJECT-ORIENTED PROGRAMMING
Static Binding
In strongly typed programming languages you have to
declare variables prior to their use
int x, y
float z
x=a /* type mismatch
Definition (Static Binding) If the type T of a variable
is explicitly associated with its name N by
declaration, we say, that N is statically bound to T.
The association process is called static binding.
OBJECT-ORIENTED PROGRAMMING
Dynamic Binding
Some languages allow to introduce variables once
they are needed
Definition (Dynamic Binding) If the type T of a
variable with name N is implicitly associated by its
content, we say, that N is dynamically bound to T.
The association process is called dynamic binding
OBJECT-ORIENTED PROGRAMMING
Packaging
collections of classes and interfaces that are
related to each other in some useful way
benefits:
the ability to organize many class definitions into a
single unit.
the "friendly" instance variables and methods are
available to all classes within the same package, but
not to classes defined outside the package
SUMMARY
A fundamental principle in object-oriented
programming to view a program as a
collection of interacting objects
Objects in a collection:
react upon receipt of messages,
change their state according to invocation of
methods which might cause other messages
sent to other objects
WHAT IS JAVA?
Late 1980s C++ was widely used to write OOP:
not a platform independent
needed to be recompiled for each different
CPUs
1991 a team of Sun Microsystems make a
platform independent software and named it
Oak 1995 Java
WHAT IS JAVA?
Java influenced by C, C++, Smalltalk
slogan named Write Once Run Anywhere
it can develop and run on any device equipped
with Java Virtual Machine (JVM).
applicable in all kinds of operating systems
associated with the World Wide Web
a platform independent programming language
can be found in a variety of devices like cell
phones, e-commerce application, PCs and almost
all network or computing devices
THE JAVA LANGUAGE
all source code written in plain text files
ending with the .java extension.
source files compiled into .class files by the
javac compiler.
.class file contains bytecodes the machine
language of the Java Virtual Machine.
java launcher tool then runs your application
with an instance of the Java Virtual Machine.
THE JAVA LANGUAGE
Java VM available on many different operating
systems, the same .class files are
capable of running on Microsoft Windows,
the Solaris
TM
Operating System (Solaris OS),
Linux, or Mac OS
THE JAVA LANGUAGE
THE JAVA PLATFORM
A platform the hardware or software
environment in which a program runs.
Java a software-only platform that runs on
top of other hardware-based platforms:
The Java Virtual Machine
The Java Application Programming Interface (API)
a large collection of ready-made software components
that provide many useful capabilities grouped in
packages
THE JAVA PLATFORM
JAVA CHARACTERISTICS
Simple, Object Oriented, and Familiar:
Simple language, easy to learn
Provides a clean and efficient object-based
development platform
Keeping the Java programming language looking
like C++ as far as possible results in it being a
familiar language
JAVA CHARACTERISTICS
Robust and Secure
Designed for creating highly reliable software
The memory management model is extremely
simple: objects are created with a new operator
The system will find many errors quickly
Security features designed into the language and
run-time system
Java technology constructs applications that can't
be invaded from outside
JAVA CHARACTERISTICS
Architecture Neutral and Portable
the Java Compiler
TM
product generates bytecodes--
an architecture neutral intermediate format
designed to transport code efficiently to multiple
hardware and software platforms
programs are the same on every platform--there are
no data type incompatibilities across hardware and
software architectures
JAVA CHARACTERISTICS
High Performance
The interpreter can run at full speed without
needing to check the run-time environment
The automatic garbage collector runs as a low-
priority background thread
JAVA CHARACTERISTICS
Interpreted, Threaded, and Dynamic
The Java interpreter can execute Java bytecodes
directly on any machine to which the interpreter
and run-time system have been ported
Java technology's multithreading capability provides
the means to build applications with many
concurrent threads of activity (results in a high
degree of interactivity for the end user)
The language and run-time system are dynamic in
their linking stages
REFERENCES
Peter Muller, Introduction to Object-Oriented
Programming Using C++, Chapters 1, 2, 3 and 4,
http://www.desy.de/gna/html/cc/Tutorial/tutorial.html
The Java Tutorials. Getting Started.
http://java.sun.com/docs/books/tutorial/getStarted/i
ndex.html

También podría gustarte