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

Object Oriented Programming

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

Object Oriented Programming

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

Object-Oriented Programming

• Procedure versus Object Oriented Programming


• Class and Object
• Inheritance, Encapsulation, and Polymorphism
Procedure-Oriented
Programming

• List of instructions to tell the computer what to do


• Instructions are then organized into functions
• Program is divided into a collection of variables, data structures, and
routines to accomplish different tasks
Object-Oriented Programming
• Programming task is broken into objects which combine
• data (known as attributes)
• behaviors/functions (known as methods)
• Two main components of OOP:
• class: blueprint to define a logical grouping of data and functions; template to
define the needed information
• object: an instance of the class with actual values
• Example:
• class: people (name, age, gender)
• object: Superman with age 33
Example: Class People
definition of class

attributes (data) of class

methods (functions) of class

instantiation of an object
greeting person1
printing name & age
Advantages of OOP
• It provides a clear modular structure for programs that enhances code
reusability.
• It provides a simple way to solve complex problems.
• It helps define more abstract data types to model real-world
scenarios.
• It hides implementation details, leaving a clearly defined interface.
• It combines data and operations.
Class
• Definition of the structure that we want (similar to function
definition); needs to be instantiated first before you can use it
• Convention: use “CapWords” for class name

inherit the attributes and methods from already defined class

special method of Python;


assign attributes directly when
creating objects
define instance methods that
will be applied to attributes
Object
• Instance of the defined class with actual values aka entities.
• Many instances of different values can be associated with a class
• All instances are independent of each other
• Thing from the real word, A car, boat, book, dental appointment, bank
account
Example: Class and Objects
Class vs Instance Attributes
• Instance attribute: belong to a specific instance (self.attribute)
• Class attributes: are shared with all instances of a class
(ClassName.attribute)

• Example: class Cars()

Instance attributes: Class attributes:


brand total number of cars in fleet
manufacturing year oldest car in fleet
mileage car with highest mileage in fleet
class attribute; defined outside all other methods
Example: Class and Objects
instance attributes; values are different for each
object

increment class attribute with each object created

use class attribute


Important other Concepts of
OOP
• Inheritance: makes OOP code more modular, easier to reuse, and
capable of building a relationship between classes

• Encapsulation: can hide some of the private details of a class from


other objects

• Polymorphism: allows us to use a common operation in different


ways
Inheriting and Extending
Methods
• Child class inherits all the methods and attributes from parent class (also called
superclass)

• Parent class is of general type class Parent():


• Child class is of specific type class Child(Parent):

• Child class may have new (extended) methods

Example: class Sensor():


class TempSensor(Sensor):
class Accelerometer(Sensor):
Example: Extending Methods
Inheriting and Method
Overriding
Definition:
• Creation of two methods with the same name but different
programming logic

Process:
• Change the parent class method in the child class by defining a
method with the same name and same number of parameters
Example: Method Overriding
Inheriting and Updating
Attributes
Definition:
• Create child class with attributes that differ from parent class

Note:
• Some attributes should remain the same
• Child class could have additional / fewer attributes

Process:
• Use super method to refer to attributes to be inherented from parent class
Example: Updating Attributes

use super method to refer to attributes of


parent class

avoid redefining all attributes explicitly


Encapsulation
• Restrict access to
methods and attributes
in a class
• Hide complex details private attributes
from the users
• Prevents data being
modified by accident

error
Example: Encapsulation

_name: should not be accessed directly


__name: cannot be accessed or modified directly
Polymorphism
• Polymorphism = multiple forms
• Allows the use of a single interface with different underlying forms
• Ability of using a single name with many forms acting differently in
different situations greatly reduces complexities.
Example: Polymorphism
Procedural Oriented Programming Object-Oriented Programming

In procedural programming, the program is divided into small parts In object-oriented programming, the program is divided into small
called functions. parts called objects.

Procedural programming follows a top-down approach. Object-oriented programming follows a bottom-up approach.

Object-oriented programming has access specifiers like private, public,


There is no access specifier in procedural programming. protected, etc.

Adding new data and functions is not easy. Adding new data and function is easy.

Procedural programming does not have any proper way of hiding data Object-oriented programming provides data hiding so it is more
so it is less secure. secure.

In procedural programming, overloading is not possible. Overloading is possible in object-oriented programming.

In procedural programming, there is no concept of data hiding and In object-oriented programming, the concept of data hiding and
inheritance. inheritance is used.

In procedural programming, the function is more important than the


data. In object-oriented programming, data is more important than function.

Procedural programming is used for designing medium-sized Object-oriented programming is used for designing large and complex
programs. programs.

Procedural programming uses the concept of procedure abstraction. Object-oriented programming uses the concept of data abstraction.

Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
References
• Robertson L.A., Simple Program Design: A step-by-step approach
• Chapter 11: An introduction to object-oriented design

• Kong Q., Siauw T., Bayen A.M., Python Programming and Numerical
Methods
• Chapter 7: Object-oriented programming

• Heinold B., A Practical Introduction to Python Programming


• Chapter 14: Object-oriented programming

You might also like