Object Oriented
Programming
Course Instructor: Dr Mrs I. Botchway Essandoh
Computer Science & Engineering Department
University of Mines & Technology (UMaT) 1
Introduction
The Concept of Programming
• What is Programming?
Do I need to be good in maths?
What about logic?
• What is Computer Programming?
Designing and building executable computer programs
Object Oriented Programming 2
Computer Programs
do real world things
on their own.
Why do we need to learn about Object-Oriented Programming?
• Object-Oriented Programming was developed because
limitations were discovered in the earlier programming
approaches.
Object Oriented Programming 3
Introduction
Evolution of Programming Techniques
• Procedural Programming (PP);
• Structured Programming; and
• Object Oriented Programming.
• Views a program as a series of steps (procedures) to be
carried out;
 Uses a list of instructions; and
 Takes inputs and returns outputs.
 E.g. C, FORTRAN, Cobol, Pascal
• Division into functions
Each function has a clearly defined purpose and a clearly
defined interface to the other functions in the program.
The idea of breaking a program into functions can further be
extended by grouping a number of functions together into a
larger entity called a module.
Object Oriented Programming 4
Procedural Programming
Object Oriented Programming 5
Procedural Programming
Main Program
Function 1 Function 3
Function 2
Function 4 Function 5
Function 8
Function 7
Function 6
• In multi-function programs, important data items are placed
as global data so that they may be accessed by all functions.
• Each function may as well have its own local data.
Object Oriented Programming 6
Procedural Programming
Global
Data
Global
Data
Global
Data
Function 1 Function 2 Function 3 Function 4
Problems with Procedural Languages
• Since every function has complete access to the global data,
a new programmer can corrupt the data accidentally;
• Cannot cope with very large project sizes (Complexity) since
it is difficult to identify the data used by a specific function;
• Difficult to modify and maintain;
• Codes are often not reusable;
• We can access the data of one function from another, hence,
there is security unfriendliness due to code exposure; and
• Program codes become difficult to write when working with a
larger project.
Object Oriented Programming 7
Procedural Programming
Causes of Problems
• Unrestricted Access to Data
 Global data/variable is allowed
 Access to data by multiple functions means many connections
between functions
 Programs become more difficult to understand
• Poor Modeling of Real World Things
 Real world things are integral collections of data
(Attributes/properties/features) and functions (Behaviour)
 e.g. a car: has data (make, model etc.) and functions (acceleration)
 Procedural languages do not tie up data with functions
Object Oriented Programming 8
Procedural Programming
Characteristics:
• Large programs are divided into smaller programs known as
functions.
• Most of the functions share global data.
• Data can move openly around the system from function to
function.
• Employs the top-down approach in program design.
The top-down programming approach
This is the process of breaking tasks into component parts
called modules and the subdivide each component module
until the lowest level of detail has been reached.
Object Oriented Programming 9
Procedural Programming
• Structured Programming can be defined as a programming
approach in which the program is made as a single
structure.
• It means that the code will execute the instruction one after
the other.
• It doesn’t support the possibility of jumping from one
instruction to some other with the help of any statement like
GOTO, etc.
• Therefore, the instructions in this approach will be executed
in a serial and structured manner.
• Organizes Procedural Programming into functions and
modules.
• E.g. Pascal, Cobol, C
Object Oriented Programming 10
Structured Programming
Object Oriented Language
• OOP was introduced to overcome the
flaws in procedural programming such
as reusability and maintainability.
• Views a program as a group of objects
that have certain properties and can
perform certain functions.
• E.g. C++, Java, C#, Python
Object Oriented Programming 11
Object-Oriented Programming
Object:
John
• Clearer, more reliable, more easily maintained programs;
• More effective way of coping with program complexity;
• It basically supports encapsulation, abstraction, inheritance
and polymorphism; and
• It also includes data hiding features therefore it is more
secure.
Object Oriented Programming 12
Goal of OOP
• The fundamental idea is to combine into a single unit both
data/variable (features) and functions that operate on the
data.
• Such a unit is called an “Object”.
• In OOP, problems are divided into the number of entities
(objects) and then builds data and functions around these
entities (objects).
• It ties the data more closely to the functions that operate on
it and protects it from accidental modification from outside
functions.
Object Oriented Programming 13
The Object-oriented Approach
• Data of an object can be accessed only by the functions
associated with that object.
• Communication of objects is done through functions.
• Data and its function are said to be encapsulated into a
single entity called a class.
• Data encapsulation and data hiding are key elements of
object-oriented languages.
Object Oriented Programming 14
The Object-oriented Approach
• If you want to modify data in an object, you know exactly
what functions interact with it (i.e. the member functions of
the object).
• This simplifies writing, debugging, and maintaining the
programs.
Object Oriented Programming 15
The Object-oriented Approach
Object Oriented Programming 16
Attributes
Methods
Methods
Attributes
Methods
Methods
Attributes
Methods
Methods
object
object
The Object-oriented Paradigm
Characteristics:
• Programs are divided into entities known as objects.
• Data is hidden and cannot be accessed by external functions.
• Objects communicate with each other through functions.
• New data and functions can be easily added whenever
necessary.
• Employs the bottom up approach in program design.
Object Oriented Programming 17
Object-Oriented Programming
OOP is suitable when
you want to group
attributes and methods
together into a handy
packet of functionality
Procedural OOP
The program is divided into
small parts called functions.
The program is divided into
small parts called objects.
There is no use of access
modifiers.
Uses access modifiers like
private, public, protected, etc.
Does not have any proper way
of hiding data so it is less
secure.
Provides data hiding so it is
more secure.
Does not have the concept of
inheritance.
Supports inheritance
Absent of code reusability It offers code reusability by
using the feature of inheritance.
Object Oriented Programming 18
Procedural Vs. OOP
• The structure of object-oriented programming include the
following:
Object Oriented Programming 19
Structure of OOP Languages
• The Object-oriented approach relies on a number of concepts
that define object-oriented languages.
Object Oriented Programming 20
Concepts of OOP Languages
OOP
Concepts
Class
Objects
Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic
Binding
Message
Passing
Object Oriented Programming 21
Classes
Class
Data/Variables
Properties/
Features
Functions
Methods/
Behavior
Object
• A collection of data and features encapsulated in a single
unit.
• A class is a user-defined data type that we can use in our
program, and it works as a blueprint for creating objects but
it doesn’t actually provide any real content itself.
• The Dog( ) class may specify that the name and age are
necessary for defining a dog, but it will not actually state
what a specific dog’s name or age is.
Object Oriented Programming 22
Classes/ Objects
think of a class as an idea for how
something should be defined
• Classes enable the creation of specific instances of the
system being modeled (Object).
• Attributes and methods are basically variables and
functions that belongs to the class. These are often referred
to as class members.
Creating a Class in Python
• Use the class keyword, followed by the class name and a
colon
Syntax
class ClassName:
# class definition
Object Oriented Programming 23
Classes/ Objects
• When a class is defined, only the specification for the object
is defined; no memory or storage is allocated. To use the data
and access functions defined in the class, we need to create
objects.
• An object is created from a class. It is an instance of a class
with actual values.
Object Oriented Programming 24
Objects
• An object is a software item that contains variables and
methods.
• They may represent a person, a place, a bank account, or
any item that a program must handle.
• When a program is executed, the objects interact by sending
messages to one another.
• Objects have two components:
• Data (attributes)
• Behaviors (functions or methods)
Object Oriented Programming 25
Objects
Instantiating Object
• To create an object of a class, type the class name, followed
by ( ). Assign the class name to the object name.
Syntax
class ClassName:
pass
objectName = ClassName()
Access Class Attributes Using Objects
• The attributes of a class can be accessed using the dot (.)
notation on the object of the class.
Object Oriented Programming 26
Objects
Object Oriented Programming 27
Example 1: An Empty Class
Object Oriented Programming
class Student:
pass
stud1 = Student()
stud1.name='Amina'
stud1.age = 20
stud1.dpt = 'Electrical'
stud1.hostel = 'Gold Refinery Hall'
print(f'Name:
{stud1.name}nAge:{stud1.age}nDepartment:{stud1.dpt}
nHostel:{stud1.hostel}n')
27
Object Oriented Programming 28
Example 2: Multiple Objects
Object Oriented Programming
class Student:
pass
stud1 = Student()
stud2 = Student()
stud1.name='Amina'
stud1.age = 20
stud1.dpt = 'Electrical'
stud1.hostel = 'Gold Refinery Hall'
stud2.name='Abena'
stud2.age = 19
stud2.dpt = 'Electrical'
stud2.hostel = 'KT Hall'
print(f'Name:
{stud1.name}nAge:{stud1.age}nDepartment:{stud1.dpt}nHostel:
{stud1.hostel}n')
print(f'Name:
{stud2.name}nAge:{stud2.age}nDepartment:{stud2.dpt}nHostel:
{stud2.hostel}')
28
Example 3: Class with Attributes
class Cars:
name = ''
make = ''
model = ''
year = 0
car1 = Cars() # Creating first object
car1.name='Toyota'
car1.make = 'Corolla'
car1.model = 'LE'
car1.year = 2016
car2 = Cars() # Creating second object
car2.name='Honda'
car2.make = 'Civic'
car2.model = 'Hybrid'
car2.year = 2018
car3 = Cars() # Creating third object
car3.name='Mercedes Benz'
car3.make = 'C class'
car3.model = '300'
car3.year = 2000
print(f"The vehicle name is
{car1.name} and it's make is
{car1.make}. nThe model is
{car1.model} and it's year
is {car1.year}n")
print(f"The vehicle name is
{car2.name} and it's make is
{car2.make}. nThe model is
{car2.model} and it's year
is {car2.year}n")
print(f"The vehicle name is
{car3.name} and it's make is
{car3.make}. nThe model is
{car3.model} and it's year
is {car3.year}")
Object Oriented Programming 30
End of Lecture 2
Object Oriented Programming

OOP-1.pptx

  • 1.
    Object Oriented Programming Course Instructor:Dr Mrs I. Botchway Essandoh Computer Science & Engineering Department University of Mines & Technology (UMaT) 1
  • 2.
    Introduction The Concept ofProgramming • What is Programming? Do I need to be good in maths? What about logic? • What is Computer Programming? Designing and building executable computer programs Object Oriented Programming 2 Computer Programs do real world things on their own.
  • 3.
    Why do weneed to learn about Object-Oriented Programming? • Object-Oriented Programming was developed because limitations were discovered in the earlier programming approaches. Object Oriented Programming 3 Introduction Evolution of Programming Techniques • Procedural Programming (PP); • Structured Programming; and • Object Oriented Programming.
  • 4.
    • Views aprogram as a series of steps (procedures) to be carried out;  Uses a list of instructions; and  Takes inputs and returns outputs.  E.g. C, FORTRAN, Cobol, Pascal • Division into functions Each function has a clearly defined purpose and a clearly defined interface to the other functions in the program. The idea of breaking a program into functions can further be extended by grouping a number of functions together into a larger entity called a module. Object Oriented Programming 4 Procedural Programming
  • 5.
    Object Oriented Programming5 Procedural Programming Main Program Function 1 Function 3 Function 2 Function 4 Function 5 Function 8 Function 7 Function 6
  • 6.
    • In multi-functionprograms, important data items are placed as global data so that they may be accessed by all functions. • Each function may as well have its own local data. Object Oriented Programming 6 Procedural Programming Global Data Global Data Global Data Function 1 Function 2 Function 3 Function 4
  • 7.
    Problems with ProceduralLanguages • Since every function has complete access to the global data, a new programmer can corrupt the data accidentally; • Cannot cope with very large project sizes (Complexity) since it is difficult to identify the data used by a specific function; • Difficult to modify and maintain; • Codes are often not reusable; • We can access the data of one function from another, hence, there is security unfriendliness due to code exposure; and • Program codes become difficult to write when working with a larger project. Object Oriented Programming 7 Procedural Programming
  • 8.
    Causes of Problems •Unrestricted Access to Data  Global data/variable is allowed  Access to data by multiple functions means many connections between functions  Programs become more difficult to understand • Poor Modeling of Real World Things  Real world things are integral collections of data (Attributes/properties/features) and functions (Behaviour)  e.g. a car: has data (make, model etc.) and functions (acceleration)  Procedural languages do not tie up data with functions Object Oriented Programming 8 Procedural Programming
  • 9.
    Characteristics: • Large programsare divided into smaller programs known as functions. • Most of the functions share global data. • Data can move openly around the system from function to function. • Employs the top-down approach in program design. The top-down programming approach This is the process of breaking tasks into component parts called modules and the subdivide each component module until the lowest level of detail has been reached. Object Oriented Programming 9 Procedural Programming
  • 10.
    • Structured Programmingcan be defined as a programming approach in which the program is made as a single structure. • It means that the code will execute the instruction one after the other. • It doesn’t support the possibility of jumping from one instruction to some other with the help of any statement like GOTO, etc. • Therefore, the instructions in this approach will be executed in a serial and structured manner. • Organizes Procedural Programming into functions and modules. • E.g. Pascal, Cobol, C Object Oriented Programming 10 Structured Programming
  • 11.
    Object Oriented Language •OOP was introduced to overcome the flaws in procedural programming such as reusability and maintainability. • Views a program as a group of objects that have certain properties and can perform certain functions. • E.g. C++, Java, C#, Python Object Oriented Programming 11 Object-Oriented Programming Object: John
  • 12.
    • Clearer, morereliable, more easily maintained programs; • More effective way of coping with program complexity; • It basically supports encapsulation, abstraction, inheritance and polymorphism; and • It also includes data hiding features therefore it is more secure. Object Oriented Programming 12 Goal of OOP
  • 13.
    • The fundamentalidea is to combine into a single unit both data/variable (features) and functions that operate on the data. • Such a unit is called an “Object”. • In OOP, problems are divided into the number of entities (objects) and then builds data and functions around these entities (objects). • It ties the data more closely to the functions that operate on it and protects it from accidental modification from outside functions. Object Oriented Programming 13 The Object-oriented Approach
  • 14.
    • Data ofan object can be accessed only by the functions associated with that object. • Communication of objects is done through functions. • Data and its function are said to be encapsulated into a single entity called a class. • Data encapsulation and data hiding are key elements of object-oriented languages. Object Oriented Programming 14 The Object-oriented Approach
  • 15.
    • If youwant to modify data in an object, you know exactly what functions interact with it (i.e. the member functions of the object). • This simplifies writing, debugging, and maintaining the programs. Object Oriented Programming 15 The Object-oriented Approach
  • 16.
    Object Oriented Programming16 Attributes Methods Methods Attributes Methods Methods Attributes Methods Methods object object The Object-oriented Paradigm
  • 17.
    Characteristics: • Programs aredivided into entities known as objects. • Data is hidden and cannot be accessed by external functions. • Objects communicate with each other through functions. • New data and functions can be easily added whenever necessary. • Employs the bottom up approach in program design. Object Oriented Programming 17 Object-Oriented Programming OOP is suitable when you want to group attributes and methods together into a handy packet of functionality
  • 18.
    Procedural OOP The programis divided into small parts called functions. The program is divided into small parts called objects. There is no use of access modifiers. Uses access modifiers like private, public, protected, etc. Does not have any proper way of hiding data so it is less secure. Provides data hiding so it is more secure. Does not have the concept of inheritance. Supports inheritance Absent of code reusability It offers code reusability by using the feature of inheritance. Object Oriented Programming 18 Procedural Vs. OOP
  • 19.
    • The structureof object-oriented programming include the following: Object Oriented Programming 19 Structure of OOP Languages
  • 20.
    • The Object-orientedapproach relies on a number of concepts that define object-oriented languages. Object Oriented Programming 20 Concepts of OOP Languages OOP Concepts Class Objects Abstraction Encapsulation Inheritance Polymorphism Dynamic Binding Message Passing
  • 21.
    Object Oriented Programming21 Classes Class Data/Variables Properties/ Features Functions Methods/ Behavior Object
  • 22.
    • A collectionof data and features encapsulated in a single unit. • A class is a user-defined data type that we can use in our program, and it works as a blueprint for creating objects but it doesn’t actually provide any real content itself. • The Dog( ) class may specify that the name and age are necessary for defining a dog, but it will not actually state what a specific dog’s name or age is. Object Oriented Programming 22 Classes/ Objects think of a class as an idea for how something should be defined
  • 23.
    • Classes enablethe creation of specific instances of the system being modeled (Object). • Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as class members. Creating a Class in Python • Use the class keyword, followed by the class name and a colon Syntax class ClassName: # class definition Object Oriented Programming 23 Classes/ Objects
  • 24.
    • When aclass is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, we need to create objects. • An object is created from a class. It is an instance of a class with actual values. Object Oriented Programming 24 Objects
  • 25.
    • An objectis a software item that contains variables and methods. • They may represent a person, a place, a bank account, or any item that a program must handle. • When a program is executed, the objects interact by sending messages to one another. • Objects have two components: • Data (attributes) • Behaviors (functions or methods) Object Oriented Programming 25 Objects
  • 26.
    Instantiating Object • Tocreate an object of a class, type the class name, followed by ( ). Assign the class name to the object name. Syntax class ClassName: pass objectName = ClassName() Access Class Attributes Using Objects • The attributes of a class can be accessed using the dot (.) notation on the object of the class. Object Oriented Programming 26 Objects
  • 27.
    Object Oriented Programming27 Example 1: An Empty Class Object Oriented Programming class Student: pass stud1 = Student() stud1.name='Amina' stud1.age = 20 stud1.dpt = 'Electrical' stud1.hostel = 'Gold Refinery Hall' print(f'Name: {stud1.name}nAge:{stud1.age}nDepartment:{stud1.dpt} nHostel:{stud1.hostel}n') 27
  • 28.
    Object Oriented Programming28 Example 2: Multiple Objects Object Oriented Programming class Student: pass stud1 = Student() stud2 = Student() stud1.name='Amina' stud1.age = 20 stud1.dpt = 'Electrical' stud1.hostel = 'Gold Refinery Hall' stud2.name='Abena' stud2.age = 19 stud2.dpt = 'Electrical' stud2.hostel = 'KT Hall' print(f'Name: {stud1.name}nAge:{stud1.age}nDepartment:{stud1.dpt}nHostel: {stud1.hostel}n') print(f'Name: {stud2.name}nAge:{stud2.age}nDepartment:{stud2.dpt}nHostel: {stud2.hostel}') 28
  • 29.
    Example 3: Classwith Attributes class Cars: name = '' make = '' model = '' year = 0 car1 = Cars() # Creating first object car1.name='Toyota' car1.make = 'Corolla' car1.model = 'LE' car1.year = 2016 car2 = Cars() # Creating second object car2.name='Honda' car2.make = 'Civic' car2.model = 'Hybrid' car2.year = 2018 car3 = Cars() # Creating third object car3.name='Mercedes Benz' car3.make = 'C class' car3.model = '300' car3.year = 2000 print(f"The vehicle name is {car1.name} and it's make is {car1.make}. nThe model is {car1.model} and it's year is {car1.year}n") print(f"The vehicle name is {car2.name} and it's make is {car2.make}. nThe model is {car2.model} and it's year is {car2.year}n") print(f"The vehicle name is {car3.name} and it's make is {car3.make}. nThe model is {car3.model} and it's year is {car3.year}")
  • 30.
    Object Oriented Programming30 End of Lecture 2 Object Oriented Programming