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

Object Oriented Programming

Object Oriented Programming ppt

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Object Oriented Programming

Object Oriented Programming ppt

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CHAPTER 1

INTRODUCTION

Compiled by Hashim S. 1
Many Aspects of Programming
• Programming is problem solving
– always trying to make computer do something useful — i.e.,
finding an optimal travel route
• Programming is controlling
– computer does exactly what you tell it to
• Programming is teaching
– computer can only “learn” to do new things if you tell it how
• Programming is creative
– must find a good solution out of many possibilities
• Programming is modelling
– describe salient (relevant) properties and behaviours of a system
of components (objects)
• Programming is abstraction
– identify important features without getting lost in detail
12/14/2024 2
What’s a Program?
• Model of complex system
– model: simplified representation of salient features of something,
either tangible or abstract
– system: collection of components that work closely together
• Sequences of instructions expressed in specific programming
language:
– syntax: grammatical rules for forming instructions
– semantics: meaning/interpretation of instructions
• Instructions written (programmed/coded) by programmer
– coded in a specific programming language
– as a result, programs cannot be ambiguous
– all instructions together are called source code
– Executed by computer by carrying out individual instructions

12/14/2024 3
Programming Languages
• Machine language
– machine is short for computing machine (i.e., computer)
– computer’s native language, sequence of zeroes and ones (binary)
– different computers understand different sequences
– hard for humans to understand: 01010001…
• Assembly language
– mnemonics for machine language
– low level: each instruction is minimal
– still hard for humans to understand:
ADD d0,d2
• High-level languages
– FORTRAN, Pascal, BASIC, C, C++, Java, etc.
– high level: each instruction composed of many low-level
instructions
– closer to English easier to read and understand:
12/14/2024 4
hypotenuse = Math.sqrt(opposite * opposite + adjacent * adjacent);
Interpreting vs. Compiling Programs
• Each type of computer only “understands” its own machine
language (zeroes and ones)
• Thus we must translate from High-level language to machine
language
– a team of experts programs a translator, called a “compiler” which
translates entirety of a High-level language program to an
executable file in computer’s native machine language.
• Running :
– compilation: Your program → executable
– execution: run executable
• machine executes your program by “running” each machine language
instruction in the executable file

12/14/2024 5
Interpreting vs. Compiling Programs
• An alternative to compiling your program is to interpret your
program
– each line of your program is translated into machine language and
immediately executed
• Like translating between natural languages
– Compiler: human translator translates book in its entirety and
then translated book is printed and read
– Interpreter: human interpreter translates each spoken statement
in sequence as speaker is speaking

• In the case of interpreter, the source code is translated


every time the
program is executed
12/14/2024 6
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 for the execution of
the program
• Types
– Unstructured Programming
– Procedural programming
– Structured Programming
– Object Oriented Programming

12/14/2024 7
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

12/14/2024 8
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

12/14/2024 9
• 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, VB, C
12/14/2024 10
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

12/14/2024 11
• Advantage
• Reuse
• easier to understand and modify

12/14/2024 12
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

12/14/2024 13
12/14/2024 14
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

12/14/2024 15
The Basic 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

12/14/2024 16
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 alike.
• Would you let a contractor build your new house without
blueprints?
• The essence of modeling is to show all pertinent detail.

12/14/2024 17
Objects
• Represents a real world entity
• Is a unique, distinct, identifiable, self-contained entity that
possesses operations and contains 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.
• Consist of attributes (data) and behaviors (methods)
▪ Objects collaborate to provide functionality by exchanging
messages
▪ The interface of the object is the attributes and methods that are
visible (available) from the outside of the object for other objects
▪ Are self-consistent, coherent, and complete.
▪ Are (usually) not very complex or large.
▪ Are as loosely coupled with other objects as possible.

12/14/2024 18
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.

12/14/2024 19
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...
12/14/2024 21
• 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

12/14/2024 23
Objects vs. Classes
• Class • Object
– Classes provide the benefit of – Has the set of operations
reusability given in the class
– objects are defined in terms – Objects provide the benefit
of classes of modularity and
information hiding

12/14/2024 24
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.
12/14/2024 25
• 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.

12/14/2024 26
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

12/14/2024 27
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 are concrete classes with their own separate


implementations
Circle and Rectangleof
arethe methods
concrete classesarea() and
with their ownPerimeter()
separate
implementations
12/14/2024 of the methods area() and Perimeter() 28
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).
12/14/2024 29
• 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

12/14/2024 30
• 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

12/14/2024 32

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy