0% found this document useful (0 votes)
4 views

Computer Programming

Computer Programming

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Computer Programming

Computer Programming

Uploaded by

Solomon Asfaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT TWO

Main Programming Paradigms


Main Programming Paradigms

1. Procedural:- describes a set of possible sequences of


actions.
 It is a First do this and next do that kind of programming.
 Actions/procedures are active, where as data is considered
passive.
 Data and methods are loosely coupled
 Follows top-down approach
2. Functional:- originates from a purely mathematical
descipline: the theory of functions.
 Evaluate expression and use the resulting value
 The focus is what the output is not about details how to get the
output
 It is all about the return type of the function
 Example: scheme
Continued...

3. Logic:- programming by declaring facts and rules


 Programs are executed by querying whether a particular fact can
be deduced from facts and rules in the program.
 Deals with the extraction of knowledge from basic facts and
relations
 Example:-
If there are two people F and M for which
father(F, X)  F is father of X
father(F, Y)  F is father of Y
mother(M, X)  M is mother of X
mother(M, Y)  M is mother of Y
male(X)  and X is male Then
brother(X, Y)  X is brother of Y
A good example is Prolog programming
Object-Oriented Programming

• Is a new way of thinking about the process of


decomposing problems and developing programming
solutions.
 Represents concepts as "objects" that have data fields
(attributes that describe the object) and associated
procedures known as methods.
 Objects are used to interact with one another to design
applications and computer programs

 An object-oriented program may be viewed as a


collection of interacting objects, as opposed to the
conventional model, in which a program is seen as a list
of tasks (subroutines) to perform
Continued...

 The data of an object can be accessed only by the


functions associated with that object.
 However, functions of one object can access the
functions of other objects.
Characterstics of OOP

 Emphasis is 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
 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.
OOP Concepts

 The fundamental concepts of OOP are as follows:


Objects
Classes
Data abstraction
Eencapsulation
Inheritance
Polymorphism
Message passing
1. Objects

 Objects are the basic run-time entities in an object-


oriented system.
 They may represent a person, a place, a bank
account, a table of data or any item that the program
has to handle.
 They can be either real objects or concepts
 Objects take up space in the memory and have an
associated address.
 When a program is executed, the objects interact by
sending messages to one another.
Continued...

 For example, if “customer” and “account” are two


objects in a program, then the customer object may
send a message to the account object requesting for
the bank balance.
 Each object contains data, and code to manipulate
the data.
 Objects can interact without having to know details
of each other’s data or code.
 It is sufficient to know the type of message accepted,
and the type of response returned by the objects.
2. Classes

 A class is a collection of objects of similar type.


 Example:- mango, apple and orange are members of
the class fruit.
 objects are variables of the type class.
 Once a class has been defined, we can create any
number of objects belonging to that class.
 Each object is associated with the data of type class
with which they are created.
Class-Object
Continued...

 Classes are user-defined data types and behave like


the built-in types of a programming language.
 The syntax used to create an object is no different
than the syntax used to create an integer object in C.
 If fruit has been defined as a class, then the
statement
Fruit mango;
will create an object mango belonging to the class fruit.
3. Data Abstraction

 Refers to the act of representing essential features


without including the background details or
explanations.
 Classes use the concept of abstraction and are defined as
a list of abstract attributes and functions to operate on
these attributes.
 They encapsulate all the essential properties of the
objects that are to be created.
 The attributes are sometimes called data members
because they hold information.
 The functions that operate on these data are sometimes
called methods or member functions.
Continued...
4. Encapsulation

 It is the act of concealing/hiding the functionality of a


class so that the internal operations are hidden from
the programmer.
 With correct encapsulation, the developer does not
need to understand how the class actually operates
in order to communicate with it via its publicly
available methods and properties; known as its
public interface.
Continued...

 The wrapping up of data and functions into a single


unit (called class) is known as encapsulation.
 Data encapsulation is the most striking feature of a
class. The data is not accessible to the outside world,
and only those functions, which are wrapped in the
class, can access it.
 These functions provide the interface between the
object’s data and the program.
 This insulation of the data from direct access by the
program is called data hiding or information
hiding.
5. Inheritance

 Inheritance is the process by which objects of one


class acquire the properties of objects of another
class.
 For example, the bird ‘robin’ is a part of the class
“flying bird’ which is again a part of the class ‘bird’.
 One class of objects takes on characterstics of
another class.
 Example:- an object belonging to customer class
might also be more general such as a person.
 An object of type clerk could also be a person
 So, these and other classes could inherit from the
general class Person
Continued...

 Classifying general classes into more specific


subclasses is referred to as a
generalization/specialization hierarchy
 When a class inherits from the general one, it
inherits attributes, methods and associations.
6. Polymorphism

 It is related to generalization/specialization hierarchies


and inheritance of methods.
 Refers to the way different objects can respond in their
own to the same message.
 Example:- a message to close a dialog box, network
connection, and a document.
 Here each object knows how to close and will do in its
own way.
 The sender does not need to know how it closes it; only
needs to know that it responds to the close message.
7. Message Passing

 An object-oriented program consists of a set of


objects that communicate with each other.
 The process of programming in an object-oriented
language, therefore, involves the following basic
steps:
1. Creating classes that define objects and their
behavior,
2. Creating objects from class definitions, and
3. Establishing communication among objects.
Continued...

 Objects communicate with one another by sending and


receiving
 information much the same way as people pass messages to
one another.
 A message for an object is a request for execution of a
procedure, and therefore will invoke a function (procedure) in
the receiving object that generates the desired result.
 Thus, message passing involves specifying the name of the
object, the name of the function (message) and the
information to be sent.
 Objects have a life cycle. They can be created and destroyed.
 Communication with an object is feasible as long as it is alive.

You might also like