Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
# Object Oriented Programming
# lets you simulate the real world
# Characteristics of OOP
# --> class(logical cant perform any action), object(are physical and can perform action), encapsulation, ploymorphism,
# abstraction, inheritence
# Class -> Flower is logical(CLASS~Abstarct : something which is not real non physical) , Rose is physical(OBJECT)
# Class Composition: Property & Method(Behaviour)
class Employee: # class
salary=5000 # variable are properties
def loan(self): # functions are methods
print("I approve loan")
sameer=Employee() # object: objectname=classname(),, created outside the class
print(sameer.salary)
sameer.loan()
5000
I approve loan
class Employee: # class
salary=5000 # variable are properties Global variable accessible everywhere
def loan(self): # functions are methods
print("I approve loan")
def reject(self):
print("Loan Rejected")
sameer=Employee() # object: objectname=classname(),, created outside the class
print(sameer.salary)
sameer.reject()
sameer.loan()
5000
Loan Rejected
I approve loan
class Employee:
def reward(self): # self is the same object who is calling the reward method
print("Awarded")
aditya=Employee()
aditya.reward()
Awarded
class Employee:
def reward(self): # self is the same object who is calling the reward method
print("Awarded")
self.company="Microsoft"
perks="Bicycle" # cannot use perks outside rewards method ## local variable
aditya=Employee()
aditya.reward()
print(aditya.company)
print(aditya.perks)
Awarded
Microsoft
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-d16b28671983> in <module>
7 aditya.reward()
8 print(aditya.company)
----> 9 print(aditya.perks)
AttributeError: 'Employee' object has no attribute 'perks'