SlideShare a Scribd company logo
Object Oriented Programming
Lec: 02
Create Multiple Objects of Python Class
• # define a class
• class Employee:
• # define an attribute
• employee_id = 0
• # create two objects of the Employee class
• employee1 = Employee()
• employee2 = Employee()
• # access attributes using employee1
• employee1.employee_id = 1001
• print(f"Employee ID: {employee1.employee_id}")
• # access attributes using employee2
• employee2.employee_id= 1002
• print(f"Employee ID: {employee2.employee_id}")
Employee ID: 1001
Employee ID: 1002
Behaviors are actions
• Behaviors are actions that can occur on an object.
• The behaviors that can be performed on a specific class of objects are
called methods. At the programming level, methods are like functions in
structured programming, like functions, methods can also accept
parameters and return values.
• Parameters to a method are a list of objects that need to be passed into
the method. These objects are used by the method to perform whatever
behavior or task it is meant to do. Returned values are the results of that
task.
Example
• Let's take the example of inventory application.
• One action that can be associated with oranges is the pick action.
pick would place the orange in a basket by updating the basket
attribute of the orange, and by adding the orange to the oranges list
on the Basket. So, pick needs to know what basket it is dealing with.
We do this by giving the pick method a basket parameter. Since our
fruit farmer also sells juice, we can add a squeeze method to Orange.
When squeezed, squeeze might return the amount of juice retrieved,
while also removing the Orange from the basket it was in.
Cont.
• Basket can have a sell action. When a basket is sold, our inventory
system might update some data on as-yet unspecified objects for
accounting and profit calculations. Alternatively, our basket of
oranges might go bad before we can sell them, so we add a discard
method. Let's add these methods to our diagram:
Hiding details and creating the public interface
• The key purpose of modeling an object in
object-oriented design is to determine
what the public interface of that object will
be.
• The interface is the collection of attributes
and methods that other objects can use to
interact with that object. They do not need,
and are often not allowed, to access the
internal workings of the object. A common
real-world example is the mobile phone.
Cont.
• Our interface to the mobile phone is the touch screen. Each action on
the mobile phone either messaging or dialing a number represents a
method that can be called on the mobile phone. When we, as the
calling object, access these methods, we do not know or care if the
mobile phone is getting its signal from an antenna.
• Similarly, we don't care what electronic signals are being sent to
adjust the volume, or whether the sound is destined to speakers or
headphones.
Encapsulation
• This process of hiding the implementation, or functional details, of an
object is suitably called information hiding. It is also sometimes referred to
as encapsulation.
• Let’s, take an example of time capsule (A time capsule is a historic cache of
goods or information, usually intended as a deliberate method of
communication with future people)
Encapsulation vs Information Hiding
• Encapsulated data is not necessarily hidden. Encapsulation is, literally,
creating a capsule and so think of creating a time capsule. If you put a
bunch of information into a time capsule, lock and bury it, it is both
encapsulated and the information is hidden.
• On the other hand, if the time capsule has not been buried and is
unlocked or made of clear plastic, the items inside it are still
encapsulated, but there is no information hiding
Abstraction
• Abstraction is another object-oriented concept related to encapsulation
and information hiding.
• Abstraction means dealing with the level of detail that is most
appropriate to a given task. It is the process of extracting a public
interface from the inner details.
A driver of a car needs to interact with
steering, gas pedal, and brakes. The
workings of the motor, drive train, and
brake subsystem don't matter to the
driver.
Cont.
• A mechanic, on the other hand,
works at a different level of
abstraction, tuning the engine
and bleeding the breaks.
Abstraction is the process of encapsulating
information with separate public and private
interfaces
Summary
• Objects typically represent nouns in the original problem, while
methods are normally verbs. Attributes can often be picked up as
adjectives, although if the attribute refers to another object that is
part of the current object, it will still likely be a noun. Name classes,
attributes, and methods accordingly.
constructor
• Most object-oriented programming languages have the concept of a
constructor, a special method that creates and initializes the object
when it is created.
The __init__()
• __init__() is the built-in method.
• All classes have a methods called __init__(), which is always executed
when the class is being initiated.
• Use the __init__() method to assign values to object properties, or other
operations that are necessary to do when the object is being created.
• The __init__() is similar to constructors in C++ and Java. It is run as soon
as an object of a class is instantiated.
The self Parameter
• The self parameter is a reference
to the current instance of the
class, and is used to access
variables that belongs to the
class.
• It does not have to be named self
, you can call it whatever you like,
but it has to be the first
parameter of any function in the
class.
class Person:
def __init__(myobject, name, age):
myobject.name = name
myobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Example 1
• Create a class named Person, use the __init__() function to assign
values for name and age:
class Bike:
def __init__(self, name, gear, speed):
self.name=name
self.gear=gear
self.speed=speed
def gear1(speed):
print(f“ push break{bike1.speed} is dangerous")
bike1=Bike("ali", 12, 70)
bike1.gear1()
Example 2
class Dog:
# class attribute
attr1 = "mammal"
# Instance attribute
def __init__(self, name):
self.name = name
def speak(self):
print("My name is
{}".format(self.name))
# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")
# Accessing class methods
Rodger.speak()
Tommy.speak()
Example 3
class Number:
def __init__(self, number):
self.number = number
def even(self, num1):
for c in range(self.number, num1):
print(f"Even numbers are: {c*2}")
a=Number(4)
a.even(14)
• Inheritance allows us to define a class that inherits all
the methods and properties from another class.
• Parent class is the class being inherited from, also
called base class.
• Child class is the class that inherits from another
class, also called derived class.
For practical purpose
• Anaconda: https://anaconda.org/
• Jupyter notebook

More Related Content

Similar to OOP02-27022023-090456am.pptx

Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Vu Tran Lam
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionSamuelAnsong6
 
Software Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iSoftware Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iTaymoor Nazmy
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basicsJavedKhan524377
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs InheritanceBlue Elephant Consulting
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
L1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfL1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfBhanuJatinSingh
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 

Similar to OOP02-27022023-090456am.pptx (20)

Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
 
Software Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-iSoftware Engineering Lec5 oop-uml-i
Software Engineering Lec5 oop-uml-i
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Ooad ch 2
Ooad ch 2Ooad ch 2
Ooad ch 2
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Ooad ch 1_2
Ooad ch 1_2Ooad ch 1_2
Ooad ch 1_2
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs Inheritance
 
01 objective-c session 1
01  objective-c session 101  objective-c session 1
01 objective-c session 1
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
advance-dart.pptx
advance-dart.pptxadvance-dart.pptx
advance-dart.pptx
 
L1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdfL1-Introduction to OOPs concepts.pdf
L1-Introduction to OOPs concepts.pdf
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessWSO2
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfmbmh111980
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisNeo4j
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandIES VE
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowPeter Caitens
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfMeon Technology
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...rajkumar669520
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamtakuyayamamoto1800
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILNatan Silnitsky
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEJelle | Nordend
 

Recently uploaded (20)

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 

OOP02-27022023-090456am.pptx

  • 2. Create Multiple Objects of Python Class • # define a class • class Employee: • # define an attribute • employee_id = 0 • # create two objects of the Employee class • employee1 = Employee() • employee2 = Employee() • # access attributes using employee1 • employee1.employee_id = 1001 • print(f"Employee ID: {employee1.employee_id}") • # access attributes using employee2 • employee2.employee_id= 1002 • print(f"Employee ID: {employee2.employee_id}") Employee ID: 1001 Employee ID: 1002
  • 3. Behaviors are actions • Behaviors are actions that can occur on an object. • The behaviors that can be performed on a specific class of objects are called methods. At the programming level, methods are like functions in structured programming, like functions, methods can also accept parameters and return values. • Parameters to a method are a list of objects that need to be passed into the method. These objects are used by the method to perform whatever behavior or task it is meant to do. Returned values are the results of that task.
  • 4. Example • Let's take the example of inventory application. • One action that can be associated with oranges is the pick action. pick would place the orange in a basket by updating the basket attribute of the orange, and by adding the orange to the oranges list on the Basket. So, pick needs to know what basket it is dealing with. We do this by giving the pick method a basket parameter. Since our fruit farmer also sells juice, we can add a squeeze method to Orange. When squeezed, squeeze might return the amount of juice retrieved, while also removing the Orange from the basket it was in.
  • 5. Cont. • Basket can have a sell action. When a basket is sold, our inventory system might update some data on as-yet unspecified objects for accounting and profit calculations. Alternatively, our basket of oranges might go bad before we can sell them, so we add a discard method. Let's add these methods to our diagram:
  • 6. Hiding details and creating the public interface • The key purpose of modeling an object in object-oriented design is to determine what the public interface of that object will be. • The interface is the collection of attributes and methods that other objects can use to interact with that object. They do not need, and are often not allowed, to access the internal workings of the object. A common real-world example is the mobile phone.
  • 7. Cont. • Our interface to the mobile phone is the touch screen. Each action on the mobile phone either messaging or dialing a number represents a method that can be called on the mobile phone. When we, as the calling object, access these methods, we do not know or care if the mobile phone is getting its signal from an antenna. • Similarly, we don't care what electronic signals are being sent to adjust the volume, or whether the sound is destined to speakers or headphones.
  • 8. Encapsulation • This process of hiding the implementation, or functional details, of an object is suitably called information hiding. It is also sometimes referred to as encapsulation. • Let’s, take an example of time capsule (A time capsule is a historic cache of goods or information, usually intended as a deliberate method of communication with future people)
  • 9. Encapsulation vs Information Hiding • Encapsulated data is not necessarily hidden. Encapsulation is, literally, creating a capsule and so think of creating a time capsule. If you put a bunch of information into a time capsule, lock and bury it, it is both encapsulated and the information is hidden. • On the other hand, if the time capsule has not been buried and is unlocked or made of clear plastic, the items inside it are still encapsulated, but there is no information hiding
  • 10. Abstraction • Abstraction is another object-oriented concept related to encapsulation and information hiding. • Abstraction means dealing with the level of detail that is most appropriate to a given task. It is the process of extracting a public interface from the inner details. A driver of a car needs to interact with steering, gas pedal, and brakes. The workings of the motor, drive train, and brake subsystem don't matter to the driver.
  • 11. Cont. • A mechanic, on the other hand, works at a different level of abstraction, tuning the engine and bleeding the breaks. Abstraction is the process of encapsulating information with separate public and private interfaces
  • 12. Summary • Objects typically represent nouns in the original problem, while methods are normally verbs. Attributes can often be picked up as adjectives, although if the attribute refers to another object that is part of the current object, it will still likely be a noun. Name classes, attributes, and methods accordingly.
  • 13. constructor • Most object-oriented programming languages have the concept of a constructor, a special method that creates and initializes the object when it is created.
  • 14. The __init__() • __init__() is the built-in method. • All classes have a methods called __init__(), which is always executed when the class is being initiated. • Use the __init__() method to assign values to object properties, or other operations that are necessary to do when the object is being created. • The __init__() is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated.
  • 15. The self Parameter • The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class. • It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class. class Person: def __init__(myobject, name, age): myobject.name = name myobject.age = age def myfunc(abc): print("Hello my name is " + abc.name) p1 = Person("John", 36) p1.myfunc()
  • 16. Example 1 • Create a class named Person, use the __init__() function to assign values for name and age: class Bike: def __init__(self, name, gear, speed): self.name=name self.gear=gear self.speed=speed def gear1(speed): print(f“ push break{bike1.speed} is dangerous") bike1=Bike("ali", 12, 70) bike1.gear1()
  • 17. Example 2 class Dog: # class attribute attr1 = "mammal" # Instance attribute def __init__(self, name): self.name = name def speak(self): print("My name is {}".format(self.name)) # Driver code # Object instantiation Rodger = Dog("Rodger") Tommy = Dog("Tommy") # Accessing class methods Rodger.speak() Tommy.speak()
  • 18. Example 3 class Number: def __init__(self, number): self.number = number def even(self, num1): for c in range(self.number, num1): print(f"Even numbers are: {c*2}") a=Number(4) a.even(14)
  • 19. • Inheritance allows us to define a class that inherits all the methods and properties from another class. • Parent class is the class being inherited from, also called base class. • Child class is the class that inherits from another class, also called derived class.
  • 20. For practical purpose • Anaconda: https://anaconda.org/ • Jupyter notebook