Chapter One
Introduction
04/27/20 1
Programming paradigm
• A computer program usually consists of two elements:
– Data – characteristics
– Code – action
• A programming paradigm
– A fundamental style of programming
– Provides the view that the programmer has about the
execution of the program
• Types
– Unstructured Programming
– Procedural programming
– Structured Programming
– Object Oriented Programming
04/27/20 2
Unstructured Programming
• consisting only of one large (usually main) program
• “main program”' stands for a sequence of commands or
statements
– data is global throughout the whole program
• disadvantages
– Complex
– The same statement sequence must be copied more than once
in the program
• Ex. COBOL, FOCAL, MUMPS
04/27/20 3
Procedural programming
• based upon the concept of procedure call
• A procedure call is used to invoke the procedure
• Procedures (routines, subroutines, methods, functions) simply contain a series of
computational steps to be carried out to solve a problem
• Is a better choice than unstructured programming
• Involves moderate complexity or require significant ease of maintainability
• The same code can be reused without copying it
• Is strongly modular or structured
04/27/20 4
• We have a single program, which is divided into small pieces
called procedures
•Advantage
• to re-use the same code at different places in the program
without copying it
• easier way to keep track of program flow
•Example
• FORTRAN, ADA
04/27/20 5
Structured Programming
• is a subset of procedural programming (also known as
modular programming)
• Enforces a logical structure on the program being written to
make it more efficient and easier to understand, and modify.
• A program is divided into several smaller parts called module
which interacts through procedure calls
• Each module can have its own data
– allows each module to manage an internal state which is
modified by calls to procedures of this module
• Employees top-down design model
– map out the overall program structure into separate subsections
• Example
– PASCAL, C
04/27/20 6
• Advantage
• Reuse
• easier to understand and modify
04/27/20 7
Object Oriented Programming - OOP
• Data and operations are grouped together
• Each object is capable of
– receiving messages,
– processing data, and
– sending messages to other objects
• Uses objects and their interaction to design applications and
computer programs
• A program is a collection of individual units, or objects
• Organizes programs around data – Object.
• write programs by modelling problem as set of collaborating
components
– you determine what the building blocks are
– then put them together so they cooperate properly
04/27/20 8
04/27/20 9
Procedural vs. Object-Oriented
• how to write the logic, not • we want to manipulate objects
how to define the data rather than the logic required
to manipulate them
• unit programming is • unit in object-oriented
function programming is objects
• separates the data of the • focus on both the data and the
program from the operation
operations
04/27/20 10
Procedural vs. Object-Oriented
• Procedural • Object Oriented
Customer, money, account
Withdraw, deposit, transfer
04/27/20 11
The Big OO Concepts
• It is a technique of modeling some kind of system based on
objects
• Basic OO concepts
Models
Objects
Classes
Encapsulation
Abstraction
Inheritance
Polymorphism
04/27/20 12
Modeling
• Successful programs solve problems in the real world.
– They correspond closely to the real world problems they solve.
– They model the problem domain and users’ activities.
• Modeling enables better communication and visualization for
all stakeholders.
• Successful OO designs almost always begin with a visual
“object model” of the problem domain, involving both the
domain experts and software designers.
• Would you let a contractor build your new house without
blueprints?
• The essence of modeling is to show all pertinent detail.
04/27/20 13
Object
• Represents a real world entity
• possesses operations and attributes (data)
• Possesses all the know-how and information it needs to perform
the services for which it was designed
– E.g. Building, Employee, Patient etc.
• Consists of attributes (data) and behaviors (methods)
The interface of the object is the attributes and methods that are
visible (available) from the outside of the object for other objects
Is self-consistent, coherent, and complete.
Is (usually) not very complex or large.
Is as loosely coupled with other objects as possible.
04/27/20 14
Characteristics of Objects
They have unique identity.
They fall into categories, or classes.
They fall into hierarchies or aggregations.
They have well defined behaviors & responsibilities.
They separate interface from implementation.
They hide their internal structures.
They have states.
They provide services.
They send messages to other objects.
They receive messages from other objects, and react
appropriately.
They delegate responsibilities to other objects.
04/27/20 15
Object Examples
• Example 1: Dogs
– States: name, color, breed, and “is hungry?”
– Behaviors: bark, run, and wag tail
• Example 2: Cars
– States: color, model, speed, direction
– Behaviors: accelerate, turn, change gears
Class
• A class is a template (blueprint)
• used to create multiple objects with similar features
• Is an abstract description of the data (characteristic) and behavior of a
collection of similar objects.
• A class can have: attributes and behaviors
– Attributes (fields): data variables which determine the status of the class
or an object
– behavior (methods): executable code of the class built from
statements.
• It allows us to manipulate/change the status of an object or access the value of
the data member
• Classes are to objects as blueprints are to houses
• The classification(s) of a collection of objects often depends on the
attributes in which you are interested.
– Examples: streets, roads, and highways...
04/27/20 17
• Classes can be considered as data type
• The properties and methods defined by a class are called
members
• Even though all dogs have four legs, and bark, each dog’s
behavior is independent of other dogs.
– For example: Dog #1 is a black Poodle, Dog #2 is a red Irish Setter
Object vs. Class
• “Class” refers to a blueprint. It defines the variables and
methods the objects support
• “Object” is an instance of a class. Each object has a class
which defines its data and behavior.
– Example : Class – Girl
• Object- Abebech, Lili, Kiki …
• The world conceptually consists of objects
• We call the object type a class
• An Object is instantiated from a Class
– Example
BankAccount myAccount ;
myAccount = new BankAccount
04/27/20 19
Encapsulation
• The only thing that an object knows about another object is
the object’s interface
• Exposing only the details that are relevant: the public
interface.
• What? not How?
• Hiding the “gears and levers”.
• Exposing only the client’s view of the object’s
responsibilities
• Protects the object from outside interference.
• Protects other objects from relying on details that are likely
to change.
• It allows the developer to separate an object's implementation
from its behavior.
04/27/20 20
• As long as the interface remains the same, any changes to the
internal implementation are transparent to the user
• Information hiding promotes loose coupling between objects
and modularity, which promotes flexible design, which
promotes reusability.
• Reduces interdependencies in the code.
– Example: Your car’s gas pedal.
• Best practice: Objects only speak to each other by method
(i.e. function) calls. They never directly access each other’s
attributes.
04/27/20 21
Polymorphism
• “The ability of two or more classes of objects to respond to
the same message, each in its own way.”
• “The ability to use any object which implements a given
interface”
• works together with encapsulation and inheritance to simplify
the flow control in an object-oriented program
• allows a sending object to communicate with different objects
in a consistent manner without worrying about how many
different implementations of a message.
• The sending object does not have to know how the receiving
object implements the message. Only the receiving objects
worries about that
04/27/20 22
Polymorphism
Polymorphic Variables Shape
area()
Shape is an abstract class
perimeter()
with no implementation of
area() and perimeter()
Rectangle
Circle
area()
area()
perimeter()
perimeter()
• Rectangle and Circle are concrete classes with their own
separate
Circle andimplementations of with
Rectangle are concrete classes the their
methods area() and
own separate
Perimeter()
implementations of the methods area() and Perimeter()
04/27/20 23
Inheritance
• allows a class to have the same behavior as another class and extend
or tailor that behavior to provide special action for specific needs.
– Example: “Class Y is like Class X except for the following
differences… ”
• Classes that inherit from a class are called subclasses (Derived
Class).
• The class a subclass inherits from are called superclass (Base
Class).
• Why use inheritance?
– When you have two types where one is necessarily an extension of the
other.
– Sometimes (but not all the time) you are going to want to ignore the
differences and look only at the what they have in common (the base
class).
04/27/20 24
• This is called generalization.
• Consider a system that can manipulate various kinds of
shapes:
• The sub-class inherits the base class’ data members and
member functions
• A sub-class has all data members of its base-class plus its own
– Base class = parent class = superclass.
– Derived class = child class = subclass.
• is-a relationship: inheritance
• A Square is-a-kind-of Rectangle (uses inheritance).
• has-a relationship: composition
• A clock has a date
04/27/20 25
• Abstraction
– is simplifying complex reality by modeling classes
appropriate to the problem, and
– working at the most appropriate level of inheritance for a
given aspect of the problem.
– Is achieved through Composition
Summary of OO Concepts
• Everything is an object.
• Computation is performed by objects communicating with
each other, requesting that other objects perform actions.
• Each object has its own memory.
• Every object is an instance of a class.
• The class is the repository (storage) for behavior associated
with an object.
• Classes can be organized into a single rooted tree structure
called inheritance hierarchy
04/27/20 27