OBJECT ORIENTED PROGRAMMING Course 1 Loredana STANCIU loredana.stanciu@aut.upt.ro Room B613
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 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 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 g
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 To program a list in a modular programming language: the interface definition the implementation ti 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 List 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 t this); BOOL list_isempty(list_handle_t this); END Singly-Linked-List-2; List List objects
MODULAR PROGRAMMING PROBLEMS 1) Explicit Creation and Destruction PROCEDURE foo() BEGIN list_handle_t t mylist; mylist <- list_create(); /* Do something with mylist */... list_destroy(mylist); END
MODULAR PROGRAMMING PROBLEMS 2) Decoupled Data and Operations A structure based on the operations rather than the data defined d 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 the compiler cannot guarantee for type safety a mechanism which h 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 t which h 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 g Object-oriented programming
OBJECT-ORIENTED ORIENTED PROGRAMMING solves some of the mentioned problems a web of interacting objects, each house-keeping its own state
OBJECT-ORIENTED ORIENTED PROGRAMMING HANDLING PROBLEMS to obtain ones own abstract view, or model, of the problem abstraction
OBJECT-ORIENTED ORIENTED PROGRAMMING HANDLING PROBLEMS 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, size, date of birth, shape, social number, room number, hair colour, hobbies problem specific properties p data the abstract employees operations
OBJECT-ORIENTED ORIENTED PROGRAMMING HANDLING PROBLEMS abstraction ti is the structuring t 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
OBJECT-ORIENTED ORIENTED PROGRAMMING PROPERTIES OF ABSTRACT DATA TYPES with abstraction ti one can create a welldefined 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
OBJECT-ORIENTED ORIENTED PROGRAMMING PROPERTIES OF ABSTRACT DATA TYPES 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
OBJECT-ORIENTED ORIENTED PROGRAMMING IMPORTANCE OF DATA STRUCTURE ENCAPSULATION 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
OBJECT-ORIENTED ORIENTED PROGRAMMING IMPLEMENTATION OF ABSTRACT DATA TYPES A class: an actual representation of an Abstract Data Type provides implementation details for the data structure used and operations 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). ( ) p yp ( ) It defines attributes and methods which implement the data structure and operations of the ADT, respectively.
OBJECT-ORIENTED 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 d by its name and ditd defines a state t which h 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 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 fthe method and the arguments of the method. Definition (Method) A method is associated with a class. An object invokes ( ) j a method as a reaction to receipt of a message.
OBJECT-ORIENTED ORIENTED PROGRAMMING Relationships between two similar classes Is-A relationship Between objects Part-Of relationship
OBJECT-ORIENTED 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 d d 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 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 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 lowpriority 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 yprovides 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
SCHEDULER C o u r s W W W W W W W W W W W W W W 1 2 3 4 5 6 7 8 9 1 1 1 1 1 0 1 2 3 4 C C C C C E1 P1 C E1 P2 C C C E2 P1 L L1 L2 L3 L4 L5 L6 L7 T1 L8 L9 L10 L11 T2 L12 a b E2 P2