###### tags: `Lesson Notes` `Python` `OOP` `PythonFlask2021`
# Intro to OOP:
- Just what is OOP?
- Object Oriented Programming thats what.
- Ok but whats the big deal
- Well when used right it keeps you from repeating code. And keeping your code more organized
## 4 Pillars of OOP
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
### Encapsulation:
- Where we keep our code grouped together into objects.
- We use classes to define what the objects are and how they will behave buy encapsulating attributes and methods in our class
```
class Dog:
def __init__(self, name):
self.name = name
def walkDog(self, location):
print(f"Taking {self.name} for a walk to {location}!")
def printDog(self):
print(f"{self.name} is his name-o")
```
### Inheritance:
- Where we pass attributes and methods from one class to a sub-class or child class.
```
class Owner:
def __init__(self, ownerName, dog):
self.ownerName = ownerName
self.dog = dog
def printOwner(self):
self.dog.dogName()
print(f"{self.ownerName} is the owner of {self.dog.name}")
```
### Polymorphism:
- Here is where we can have a different version of a method in the child class from the parent class
```
class Owner:
def __init__(self, ownerName, dog):
self.ownerName = ownerName
self.dog = dog
def takeWalk(self, location):
self.dog.walkDog(location)
print(f"{self.ownerName} is taking {self.dog.name} for a walk to {location}")
def printOwner(self):
self.dog.dogName()
print(f"{self.ownerName} is the owner of {self.dog.name}")
```
### Abstraction:
- This is an extension of Encapsulation
- It is where we can create a new class that has a more clean look because we hide things that it doesn't need to know.
```
class PetOwner:
def __init__(self,petOwner):
self.petOwner = petOwner
self.pet = Dog("Copper Tone")
def walk(self):
self.pet.dogName()
print(f"{self.petOwner} is walking {self.pet.name}")
```
# Link to code:
- [OOP Intro](https://pythontutor.com/visualize.html#code=class%20Dog%3A%0A%20%20%20%20def%20__init__%28self,%20name%29%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%20%20%20%20def%20walkDog%28self,%20location%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22Taking%20%7Bself.name%7D%20for%20a%20walk%20to%20%7Blocation%7D!%22%29%0A%20%20%20%20def%20printDog%28self%29%3A%0A%20%20%20%20%20%20%20%20print%28f%22%7Bself.name%7D%20is%20his%20name-o%22%29%0A%20%20%20%20def%20dogName%28self%29%3A%0A%20%20%20%20%20%20%20%20return%20self%0A%20%20%20%20%20%20%20%20%0Acopper%20%3D%20Dog%28%22Copper%22%29%0Acopper.printDog%28%29%0Acopper.walkDog%28%22The%20Park%22%29%0A%0Aclass%20Owner%3A%0A%20%20%20%20def%20__init__%28self,%20ownerName,%20dog%29%3A%0A%20%20%20%20%20%20%20%20self.ownerName%20%3D%20ownerName%0A%20%20%20%20%20%20%20%20self.dog%20%3D%20dog%0A%20%20%20%20def%20takeWalk%28self,%20location%29%3A%0A%20%20%20%20%20%20%20%20self.dog.walkDog%28location%29%0A%20%20%20%20%20%20%20%20print%28f%22%7Bself.ownerName%7D%20is%20taking%20%7Bself.dog.name%7D%20for%20a%20walk%20to%20%7Blocation%7D%22%29%0A%20%20%20%20def%20printOwner%28self%29%3A%0A%20%20%20%20%20%20%20%20self.dog.dogName%28%29%0A%20%20%20%20%20%20%20%20print%28f%22%7Bself.ownerName%7D%20is%20the%20owner%20of%20%7Bself.dog.name%7D%22%29%0A%20%20%20%20%20%20%20%20%0Amelissa%20%3D%20Owner%28%22Melissa%22,%20copper%29%0Amelissa.printOwner%28%29%0Amelissa.takeWalk%28%22The%20Park%22%29%0A%0A%0Aclass%20PetOwner%3A%0A%20%20%20%20def%20__init__%28self,petOwner%29%3A%0A%20%20%20%20%20%20%20%20self.petOwner%20%3D%20petOwner%0A%20%20%20%20%20%20%20%20self.pet%20%3D%20Dog%28%22Copper%20Tone%22%29%0A%20%20%20%20def%20walk%28self%29%3A%0A%20%20%20%20%20%20%20%20self.pet.dogName%28%29%0A%20%20%20%20%20%20%20%20print%28f%22%7Bself.petOwner%7D%20is%20walking%20%7Bself.pet.name%7D%22%29%0A%20%20%20%20%20%20%20%20%0Ahoneybee%20%3D%20PetOwner%28%22Melissa%22%29%0Ahoneybee.walk%28%29%0A&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)