SlideShare a Scribd company logo
1 of 20
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
 
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
 
CSc investigatory project
CSc investigatory projectCSc investigatory project
CSc investigatory projectDIVYANSHU KUMAR
 

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
 
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
 
CSc investigatory project
CSc investigatory projectCSc investigatory project
CSc investigatory project
 

Recently uploaded

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

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