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: