44
OBJECT ORIENTED PROGRAMMING Course 1 Course 1 Loredana STANCIU [email protected] Room B613

OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

Embed Size (px)

Citation preview

Page 1: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

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 1Course 1Loredana STANCIU

[email protected] B613

Page 2: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

A SURVEY OF PROGRAMMING TECHNIQUESA SURVEY OF PROGRAMMING TECHNIQUES

Unstructured programmingUnstructured programming

Page 3: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

UNSTRUCTURED PROGRAMMINGUNSTRUCTURED PROGRAMMING

Small and simple programs Small and simple programs consisting only of one main programprogramA sequence of commands or statements which modify or statements which modify data (global) throughout the whole programthe whole program

Page 4: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

UNSTRUCTURED PROGRAMMINGUNSTRUCTURED PROGRAMMING

Disadvantage: in large programs using the Disadvantage: in large programs using the same statement sequence

Idea

to extract these sequences name them and to extract these sequences, name them and offering a technique to call and return from these proceduresthese procedures

Page 5: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

A SURVEY OF PROGRAMMING TECHNIQUESA SURVEY OF PROGRAMMING TECHNIQUES

Unstructured programmingUnstructured programmingProcedural programming

Page 6: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

PROCEDURAL PROGRAMMINGPROCEDURAL PROGRAMMING

To combine returning gsequences of statements into one single placeA procedure call is used to invoke the procedureWith parameters andsubprocedures programs can now be written more structured and error free

Page 7: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

PROCEDURAL PROGRAMMINGPROCEDURAL PROGRAMMING

A single program divided A single program divided into small pieces called proceduresproceduresTo enable usage of general procedures or general procedures or groups of procedures in other programs they other programs, they must be separately availableavailable

Page 8: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

A SURVEY OF PROGRAMMING TECHNIQUESA SURVEY OF PROGRAMMING TECHNIQUES

Unstructured programmingUnstructured programmingProcedural programmingM d l g i gModular programming

Page 9: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

MODULAR PROGRAMMINGMODULAR PROGRAMMING

Procedures of a common functionality are grouped together into separate modulesA program is now divided into several smaller parts which interact through

d ll d procedure calls and which form the whole program program

Page 10: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

EXAMPLE – HANDLING SINGLE LISTSEXAMPLE HANDLING SINGLE LISTS

To program a list in a modular programming language:language:

the interface definitionth i l t ti filthe implementation file

Page 11: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

EXAMPLE – HANDLING SINGLE LISTSEXAMPLE HANDLING SINGLE LISTS

/* * Interface definition for a module which implements * a singly linked list for storing data of any type.*/ MODULE Singly-Linked-List-1MODULE Singly-Linked-List-1

BOOL list_initialize();BOOL list_append(ANY data); BOOL list_delete();list_end();ANY list_getFirst();

Hiding information_g ();

ANY list_getNext();BOOL list_isEmpty();

END Singly Linked List 1 END Singly-Linked-List-1

Page 12: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

EXAMPLE – HANDLING MULTIPLE LISTSEXAMPLE 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 li d(li h dl hi ANY d )BOOL list_append(list_handle_t this, ANY data);ANY list_getFirst(list_handle_t this);ANY list getNext(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

END Singly Linked List 2;

Page 13: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

MODULAR PROGRAMMING PROBLEMSMODULAR PROGRAMMING PROBLEMS

1) Explicit Creation and Destruction1) Explicit Creation and DestructionPROCEDURE foo() BEGIN

list handle t myList;list_handle_t myList;myList <- list_create();/* D thi ith Li t *//* Do something with myList */...list_destroy(myList);

END

Page 14: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

MODULAR PROGRAMMING PROBLEMSMODULAR PROGRAMMING PROBLEMS

2) Decoupled Data and Operations2) Decoupled Data and OperationsA structure based on the operations

th th th d t d fi d rather than the data defined operations specify the data to be usedIn object-orientation, structure is organized by the data modules group organized by the data modules group data representations together

Page 15: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

MODULAR PROGRAMMING PROBLEMSMODULAR PROGRAMMING PROBLEMS

3) Missing Type Safety3) Missing Type Safetythe compiler cannot guarantee for type

f t h i hi h ll t safety a mechanism which allows to specify on which data type the list should be definedlist_handle_t<Apple> list1; /* a list of apples */_ _ pp ; / pp /list_handle_t<Car> list2; /* a list of cars */

Page 16: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

MODULAR PROGRAMMING PROBLEMSMODULAR PROGRAMMING PROBLEMS

4) Strategies and Representation4) Strategies and Representationa cursor is used to point to the current

l t t i g t t g hi h element a traversing strategy which defines the order in which the elements of the data structure are to be visitedto separate the actual representation or to separate the actual representation or shape of the data structure from its traversing strategytraversing strategy

Page 17: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

A SURVEY OF PROGRAMMING TECHNIQUESA SURVEY OF PROGRAMMING TECHNIQUES

Unstructured programmingUnstructured programmingProcedural programmingM d l g i gModular programmingObject-oriented programming

Page 18: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

solves some of the solves some of the mentioned problemsproblemsa web of interacting objects each objects, each house-keeping its own stateown state

Page 19: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMINGHANDLING PROBLEMS

to obtain ones own abstract view, or ,model, of the problemp

abstractionabstraction

Page 20: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

t d fi ti f th bl

HANDLING PROBLEMS

to define properties of the problem:the data which are affected andthe operations which are identified

an example: the administration of employees in an institution:

name, size, date of birth, shape, social number, room number, hair colour, hobbies

problem specific properties datap p p pthe abstract employees operations

Page 21: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

b t ti i th t t i g f

HANDLING PROBLEMS

abstraction is the structuring of a nebulous problem into well-defined entities by defining their data and operationspcombine data and operations which are notdecoupled from each otherdecoupled from each other

Page 22: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

ith b t ti t ll

PROPERTIES OF ABSTRACT DATA TYPES

with abstraction one can create a well-defined entity:

Define the data structure of a set of itemsThe data structure can only be accessed with defined operations interface exported by the entity

abstract data type

Page 23: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

1) It exports a typePROPERTIES OF ABSTRACT DATA TYPES

1) It exports a type2) It exports a set of operations. This set is called interface3) Operations of the interface are the one and only access mechanism to the type's data structure4) Axioms and preconditions define the application domain of the typepp yp

Page 24: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

Encapsulation The principle of hiding the

IMPORTANCE OF DATA STRUCTURE ENCAPSULATION

Encapsulation = The principle of hiding the used data structure and to only provide a well defined interfacewell-defined interfaceThe 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

Page 25: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

A class:IMPLEMENTATION OF ABSTRACT DATA TYPES

A class:an actual representationof an Abstract Data Type

class Integer { attributes:of an Abstract Data Type

provides implementation details for the data

attributes: int i

methods:setValue(int n)details for the data

structure used and operations

Integer addValue(Integer j)}

operations

Definition (Class) A class is the implementation of an abstract data type (ADT). ( ) p yp ( )It defines attributes and methods which implement the data structure andoperations of the ADT, respectively.

Page 26: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

An object:uniquely identifiable by a namethe 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 uniquelyid tifi d b it d it d fi t t hi h i t d b th lidentified by its name and it defines a state which is represented by the valuesof its attributes at a particular time

Definition (Behaviour) The behaviour of an object is defined by the set ofmethods which can be applied on it.

Page 27: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

A running program is a pool of objects h bj d d d d where objects are created, destroyed and

interacting through messagesInteger 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 ofits methods. A message therefore contains:

th f th th d d– the name of the method and– the arguments of the method.

Definition (Method) A method is associated with a class. An object invokes( ) ja method as a reaction to receipt of a message.

Page 28: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

Relationshipsbetween two similar classes — Is-A relationship

Between objects — Part-Of relationship

Page 29: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

OBJECT-ORIENTED PROGRAMMINGOBJECT ORIENTED PROGRAMMING

Packagingcollections of classes and interfaces that are related to each other in some useful waybenefits:

the ability to organize many class definitions into a single unitsingle unit.the "friendly" instance variables and methods are available to all classes within the same package, but not to classes defined outside the package

Page 30: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

SUMMARYSUMMARY

A fundamental principle in object-oriented i i programming — to view a program as a

collection of interacting objectsObjects 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

Page 31: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

WHAT IS JAVA?WHAT IS JAVA?

Late 1980s C++ was widely used to write OOP:Late 1980s C was widely used to write OOP:not a platform independent

d d t b il d f h diff t 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

Page 32: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

WHAT IS 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 systemsassociated with the World Wide Weba platform independent programming languagecan be found in a variety of devices like cell phones, e-commerce application, PCs and almost

ll t k ti d iall network or computing devices

Page 33: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

THE JAVA LANGUAGETHE JAVA LANGUAGE

all source code written in plain text files all source code written in plain text files ending with the .java extension. source files compiled into class files by the source files compiled into .class files by the javac compiler.

l fil t i b t d th hi .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.

Page 34: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

THE JAVA LANGUAGETHE JAVA LANGUAGE

Java VM available on many different operatingJava VM available on many different operatingsystems, the same .class files are capable of running on Microsoft Windows, p g ,the Solaris TM Operating System (Solaris OS),Linux, or Mac OS

Page 35: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

THE JAVA LANGUAGETHE JAVA LANGUAGE

Page 36: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

THE JAVA PLATFORMTHE JAVA PLATFORM

A platform — the hardware or software A platform the hardware or software environment in which a program runs.Java a software only platform that runs on Java — a software-only platform that runs on top of other hardware-based platforms:

The Java Virtual MachineThe Java Virtual MachineThe Java Application Programming Interface (API)

a large collection of ready made software components a large collection of ready-made software components that provide many useful capabilities grouped in packages

Page 37: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

THE JAVA PLATFORMTHE JAVA PLATFORM

Page 38: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

JAVA CHARACTERISTICSJAVA CHARACTERISTICS

Simple, Object Oriented, and Familiar:Simple, Object Oriented, and Familiar:Simple language, easy to learnProvides a clean and efficient object-based Provides a clean and efficient object-based development platformKeeping the Java programming language looking Keeping the Java programming language looking like C++ as far as possible results in it being a familiar language

Page 39: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

JAVA CHARACTERISTICSJAVA CHARACTERISTICS

Robust and Secure Robust and Secure Designed for creating highly reliable softwareThe memory management model is extremely The memory management model is extremely simple: objects are created with a new operator

The system will find many errors quicklyThe system will find many errors quicklySecurity features designed into the language and run-time systemrun time systemJava technology constructs applications that can't be invaded from outside

Page 40: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

JAVA CHARACTERISTICSJAVA CHARACTERISTICS

Architecture Neutral and Portable Architecture Neutral and Portable the Java CompilerTM product generates bytecodes--an architecture neutral intermediate format an architecture neutral intermediate format designed to transport code efficiently to multiple hardware and software platformsprograms are the same on every platform--there are no data type incompatibilities across hardware and software architectures

Page 41: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

JAVA CHARACTERISTICSJAVA CHARACTERISTICS

High PerformanceHigh PerformanceThe interpreter can run at full speed without needing to check the run-time environmentneeding to check the run time environmentThe automatic garbage collector runs as a low-priority background threadp y g

Page 42: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

JAVA CHARACTERISTICSJAVA CHARACTERISTICS

Interpreted, Threaded, and Dynamic Interpreted, Threaded, and Dynamic The Java interpreter can execute Java bytecodesdirectly on any machine to which the interpreter directly on any machine to which the interpreter and run-time system have been portedJava technology's multithreading capability provides gy g p y pthe 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 h i li ki their linking stages

Page 43: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

REFERENCESREFERENCES

Peter Muller, Introduction to Object-Oriented , jProgramming Using C++, Chapters 1, 2, 3 and 4, http://www.desy.de/gna/html/cc/Tutorial/tutorial.htmlThe Java Tutorials. Getting Started. http://java.sun.com/docs/books/tutorial/getStarted/index.html

Page 44: OBJECT ORIENTED PROGRAMMING - Politehnica …adrianaa/teaching/OOP/OOP - C1.pdf · object-oriented programming ... what is java? ´late 1980s clate

SCHEDULERSCHEDULER

W W W W W W W W W W W W W W1 2 3 4 5 6 7 8 9 1

011

12

13

14

C C C C C E1 C C C C E2 E2Cour

C C C C C E1 P1

CE1P2

C C C E2P1

E2P2

rs

L L1 L2 L3 L4 L5 L6 L7 T1 L8 L9 L10 L11 T2 L12ab