Event Driven Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Principles of Object-Oriented

Programming

Prepared by Muriithi Nicholas 1


Software Evolution

• Software technology has evolved through a


series of phases during the last 5 decades
• The phases include
1. Machine language
2. Assembly language
3. Procedure oriented
4. Object-oriented programming
• Procedure-oriented programming was the most
popular phase till recently
Prepared by Muriithi Nicholas 2
Procedure-oriented programming

• Procedure-oriented programming (POP)


employs top-down programming approach
where a program is viewed as a sequence of
tasks to be performed
• A number of functions are written to implement
these tasks
• POP has two major drawbacks

Prepared by Muriithi Nicholas 3


Drawbacks of POP

1. Data move freely around the program and are


therefore vulnerable to changes caused by any
function in the program
2. It does not model very well the real-world
problems

Prepared by Muriithi Nicholas 4


Characteristics exhibited by POP

• Emphasis is on doing things (algorithms)


• Large programs are divided into smaller
programs known as functions
• Most of the functions share global data
• Data move openly around the system from
function to function
• Functions transform data from one form to
another
• Employs top-dowPnrepaaredpbypStrepohenaKcamhandain program design 5
Object-Oriented Programming

• Object-Oriented Programming (OOP) was


invented to overcome the drawbacks of POP
• It employs bottom-up programming approach
• It treats data as a critical element and does not
allow it to move freely around the system
• It ties data more closely to the functions that
operate on it in a data structure called a class

Prepared by Muriithi Nicholas 6


Striking features of OOP

• Emphasis on data rather than procedure


• Programs are divided into what are known as
objects
• Data structures are designed such that they
characterize the objects
• Functions that operate on the data of an object
are tied together in the data structure. This
feature is called data encapsulation

Prepared by Muriithi Nicholas 7


Striking features of OOP Cont…

• Data is hidden and cannot be accessed by


external functions
• Objects may communicate with each other
through functions
• New data and functions can be easily added
whenever necessary
• Follows bottom-up approach in program design

Prepared by Muriithi Nicholas 8


Basic concepts of OOP

• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Prepared by Muriithi Nicholas 9
Objects

• Objects: an object is an instance of a class.


Objects contain data and code to manipulate
the data
Classes

• We just mentioned that objects contain data,


and code to manipulate that data.
• The entire set of data and code of an object can
be made a user-defined data type with the help
of a class.
• A class is a collection of objects of similar type
• E.g. mango, apple and orange are members of
the class fruit
Encapsulation

• The wrapping up of data and functions into a


single unit (called class) is known as
encapsulation.
• With encapsulation, data is not accessible to the
outside world, and only those functions wrapped
in the class can access it.
• The insulation of the data from direct access by
the program is called data hiding or information
hiding.
Data Abstraction & Encapsulation

• Abstraction refers to the act of representing


essential features without including the
background details or explanations
• Classes use the concept of abstraction and they
are therefore, known as Abstract Data Types
• The wrapping of data and functions into a single
unit (called class) is known as encapsulation
Inheritance

• Process by which objects of one class acquire


the properties of objects of another class.
• Each derived class shares common
characteristics with the class from which it is
derived
• In OOP, the concept of inheritance provides the
idea of reusability i.e. we can add additional
features to an existing class without modifying it
Polymorphism

• A Greek term meaning ability to take more than


one form
• An operation may exhibit different behaviours in
different instances depending upon the types of
data used.
• E.g. consider the operation of addition. For two
numbers, the operation will generate a sum. If
the operands are strings, the operation would
produce a third string.
Polymorphism Cont…

• The process of making an operator exhibit


different behaviours in different instances is
called operator overloading
• A single function can be used to perform
different tasks and this is known as function
overloading.
• See fig. 1 below
Polymorphism Cont… fig. 1

Shape

Draw()

Circle object Box object Triangle object

Draw (circle) Draw (box) Draw (triangle)


Polymorphism Cont…

• Polymorphism plays an important role in


allowing objects having different internal
structures to share the same external interface
• This means that a general class of operations
may be accessed in the same manner even
though specific actions associated with each
operation may differ.
• Polymorphism is extensively used in
implementing inheritance
Dynamic Binding

• Binding refers to linking of a procedure call to


the code to be executed in response to the call.
• Dynamic binding (late binding) means that the
code associate with a given procedure call is not
known until the time of the call at run-time
• Dynamic binding is associated with
polymorphism and inheritance.
Dynamic Binding Cont…

• Consider the procedure Draw () in Fig 1 above.


By inheritance, every object will have this
procedure
• However, its algorithm is unique to each object
and so the Draw() procedure will be redefined in
each class that defines the object
• At run-time, the code matching the object under
current reference will be called
Static Binding

• Also known as early binding


• The functions are bound to the code to be
executed at compile time
Message Passing

• The process of programming in object-oriented


language involves
1. Creating classes
2. Creating objects
3. Establishing communication among objects
• Objects communication with each other by
sending and receiving messages
• A message for an object is a request for
execution of a procedure
Message Passing Cont…

• Message passing involves specifying the name


of the object, the name of the function
(message) and the information to be sent.
employee.salary(name)
Object message information

• Objects have a life cycle. They can be created


and destroyed. Communication with an object is
feasible as long as it is alive.
4 Main Principles of OOP

1. Data Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Benefits of OOP

• Through inheritance, we can eliminate


redundant code and extend the existing classes
• We can build programs from the standard
working modules instead of starting from scratch
• The principle of data hiding helps build secure
programs
• Its possible to have multiple instances of an
object to co-exist without interference
• Its possible to map objects in the problem
domain to those in the program
Benefits of OOP Cont…

• Its easy to partition the work in a project based


on objects
• The data-centered design approach enables us
to capture more details of a model in
implementable form
• Object oriented systems can be easily upgraded
from small to large systems
• Message passing makes the interface
descriptions with external systems much simpler
• Software complexity can be easily managed
Object-Oriented Languages

• Programming languages should support OOP


concepts to claim they are object-oriented
• Depending upon the features they support,
they can be classified into
1. Object-based programming languages
2. Object-oriented programming languages
Object-based programming
languages
• A programming style that supports
encapsulation and object identity. Major features
that are required for object-based programming
are:
– Data encapsulation
– Data hiding and access mechanisms
– Automatic initialization and clear-up of objects
– Operator overloading
• Ada is a typical object-based programming
language
Object-oriented programming
languages

• Incorporates all of object-based features +


inheritance + dynamic binding
• Languages that support these features include
C++, Smalltalk, Object Pascal and Java
Applications of OOP

• Real-time systems
• Simulation and modeling
• Object-oriented databases
• Hypertext, hypermedia and expertext
• AI and expert systems
• Neural Networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems

You might also like