Object-Oriented Programming OOP
description
Transcript of Object-Oriented Programming OOP
Object-Oriented Programming OOP
John GilliganSchool of Computing DIT Kevin St
What is a program? “A program is a text which evokes
computation” Edsgar Dijkstra
As we will see there are many kinds and styles of program.
What does it mean to evoke computation?
Well there is a program and something is computed when the program is executed.
Ideally the computation should achieve something that we want when the program is run. The executed program should meet our requirements.
There may be an informal or formal specification of these requirements
Clearly there is a relationship between specification and program
SpecSpec ProgramProgram
From Specification to Program Different Approaches to Programming vary in
how they consider this relationship. Typically the specification describes the problem
in terms of inputs and outputs. That is the input conditions or preconditions
describe conditions or the state that holds now The output conditions or postcondition describes
the desired conditions or result that will hold after the problem has been solved or the program executed.
Verification The program must be able to match the
specification , that is do what its supposed to.
To prove that it does, it must be shown that the output state is reachable from the input state given the program.
This is called program verification.
There are many ways of developing programs
Traditional programming, called imperative programming, produces a sequence of statements which bring you from input to output conditions.
There are many formal verification techniques such as Dijkstras weakest precondition semantics.
Imperative programs tell you how to achieve a solution. They implement an algorithm .i.e. a sequence of steps which yield a solution.
Declarative Programming The major alternative to imperative programming
is declarative programming which says what is to be done not how to do it.
Examples of declarative programming include functional and logic programming such as Prolog.
In logic programming computation is produced as a by-product of proving that the output can be derived from the input.
In a pure functional language, such as Haskell, all functions are without side effects, and no explicit state or state changes exist at all.
So how does Object Oriented Programming fit into this
Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs.
Programming techniques may include features such as encapsulation, modularity, polymorphism, and inheritance.
Many modern programming languages now support OOP.
Imperative Versus OOP The big 2 approaches to Programming are
Imperative and Object Oriented
Let us look at these in more detail.
History of Imperative programming The earliest imperative languages were the machine languages of the
original computers. FORTRAN was a compiled language that allowed named variables,
complex expressions, subprograms, and many other features now common in imperative languages.
In the late 1950s and 1960s, ALGOL was developed in order to allow mathematical algorithms to be more easily expressed, and even served as the operating system's target language for some computers.
COBOL (1960) and BASIC (1964) were both attempts to make programming syntax look more like English.
In the 1970s, Pascal was developed by Niklaus Wirth C was created by Dennis Ritchie while he was working at Bell
Laboratories. Wirth went on to design Modula-2, and Oberon.
Features of Imperative Languages
Variables and Constant Declarations Assignment Statements If Statements Loops Arrays
Example of imperative program piece int is_leap_year(int year)
{ int result;
if ( (year%4) != 0 ) // or: if ( year%4 ) result = FALSE; // means: if year is not divisible by 4 else if ( (year%400) == 0 ) // or: if ( !(year%400) ) result = TRUE; // means: if year is divisible by 400 else if ( (year%100) == 0 ) // or: if ( !(year%100) ) result = FALSE; // means: if year is divisible by 100 else // (but not by 400, since that case result = TRUE; // considered already) return ( result );}
And another m_fMin = 10000; m_fMax = 0; int i; for ( i = 0; i < ARRAY_SIZE; i++ ) { m_fResultArray[i] = sqrt(m_fInitialArray[i] * 2.8f); if ( m_fResultArray[i] < m_fMin ) m_fMin = m_fResultArray[i]; if ( m_fResultArray[i] > m_fMax ) m_fMax = m_fResultArray[i]; }}
Advantages of Imperative Programming
Works Follows intuitive algorithmic reasoning Can be made modular Can be documented Good proof techniques Widely accepted
Disadvantages of imperative programming
Can be messy Poor for re-use Data types buried in code (millenium Bug) Arbitrary variable names Allows bad programmers to have long
unwieldy programs Maintenance a high cost
As an Alternative Consider Object Oriented Programming
The term “object-oriented” was coined by Alan Kay in 1967
Roots Object-oriented programming can trace its roots to the
1960s. As hardware and software became increasingly complex,
quality was often compromised. Researchers studied ways in which software quality could
be maintained. Object-oriented programming was deployed in part as an attempt to address this problem by strongly emphasizing discrete units of programming logic and re-usability in software.
Computer programming methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) containing all the information needed within its own data structure for manipulation.
OOP Object-oriented programming may be seen as a collection of
cooperating objects, as opposed to a traditional view in which a program may be seen as a group of tasks to compute ("subroutines"). In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects.
Each object can be viewed as an independent little machine with a distinct role or responsibility. The actions or "operators" on the objects are closely associated with the object.
For example, in object oriented programming, the data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or "class"). The traditional approach tends to view and consider data and behavior separately.
At First The Simula programming language was the first to
introduce the concepts underlying object-oriented programming (objects, classes, subclasses, virtual methods, coroutines, garbage collection, and discrete event simulation) as a superset of Algol. Simula was used for physical modeling, such as models to study and improve the movement of ships and their content through cargo ports. Smalltalk was the first programming language to be called "object-oriented".
History The 1980s saw a rapid growth in interest in object-oriented programming.
These languages were imperative in style, but added features to support objects. The last two decades of the 20th century saw the development of a considerable number
of such programming languages. Smalltalk, originally conceived by Alan Kay in 1969, was released in 1980 by the Xerox
Palo Alto Research Center. Drawing from concepts in another object-oriented language — Simula (which is
considered to be the world's first object-oriented programming language, developed in the late 1960s) — Bjarne Stroustrup designed C++, an object-oriented language based on C. C++was first implemented in 1985.
In the late 1980s and 1990s, the notable imperative languages drawing on object-oriented concepts were Perl, released by Larry Wall in 1987; Python, released by Guido van Rossum in 1990; PHP, released by Rasmus Lerdorf in 1994; Java, first released by Sun Microsystems in 1994 and Ruby, released in 1995 by Yukihiro “matz” Matsumoto.
JAVA In the past decade Java has emerged in wide
use partially because of its similarity to C and to C++, but perhaps more importantly because of its implementation using a virtual machine that is intended to run code unchanged on many different platforms.