OOP(Object-oriented programming)
inheritance
class Parent:
def __init__(self):
self.eyes = "green"
class Child(Parent):
def __init__(self):
super().__init__()
self.age = 7
child = Child()
print(child.eyes)
print(child.age)
Output:
green
7
class Greetings:
def greet(self):
print("Hi!")
class Person(Greetings):
name = "George"
p = Person()
p.greet()
Output:
Hi!
class Car:
def start_car(self):
print("Starting car")
class Hybrid(Car):
def charge(self):
print("Using fuel to charge battery")
class Electric(Car):
def fuel(self):
print("No fuel necessary")
prius = Hybrid()
electric = Electric()
prius.start_car()
electric.start_car()
prius.charge()
electric.fuel()
Output:
Starting car
Starting car
Using fuel to charge battery
No fuel necessary
class Dog:
def bark(self):
print("Woof!")
class Cat:
def meow(self):
print("Meow!")
pet1 = Cat()
pet2 = ()
pet1.meow();
pet2.bark();
Output:
Meow!
Woof!
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hi!")
class Nurse(Person):
def __init__(self, name, age):
super().__init__("Nurse " + name, age)
def intro(self):
print("Hi, I'm", self.name)
person1 = Nurse("Sam", 23)
person2 = Person("Tom", 30)
person1.intro()
person2.greet()
Output:
Hi, I'm Nurse Sam
Hi!
Making a subclass:
super().init(name, age)
to use existing constructors to set the same properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hi!")
class Student(Person):
def __init__(self, name, age, major):
super().__init__(name, age)
self.major = major
student = Student("Sam", 23, "chemistry")
print()
student.greet()
Output:
chemistry
Hi!
class Phone:
def call(self, contact):
print("Calling " + contact)
phone = Phone()
phone.call("Mom")
Output:
Calling Mom
abstraction
class Car:
def __init__(self):
self.on = False
def injectFuel(self):
print('Spraying fuel')
def igniteFuel(self):
print('Boom!')
def startUp(self):
self.on = True
while self.on:
self.injectFuel()
self.igniteFuel()
class IceCreamMaker:
def churn(self):
print('Churning cream')
def freeze(self):
print('Freezing cream')
def makeIceCream(self):
self.churn()
self.freeze()
iceCreamMaker = IceCreamMaker()
iceCreamMaker.makeIceCream()
Output:
Churning cream
Freezing cream
class Slideshow:
def __init__(self, slides):
self.slides = slides
self.current = 1
def viewNextSlide(self):
self.current += 1
def play(self):
while self.current <= self.slides:
print('Slide', self.current)
self.viewNextSlide()
slideshow = Slideshow(5)
slideshow.play()
Output:
Slide 1
Slide 2
Slide 3
Slide 4
Slide 5
Methods represent behaviors, for example displaying text in the console like the following:
class Feline:
def speak(self):
print("Meow")
class Cat(Feline):
def lick(self):
print("Licking paw")
cat = Cat()
cat.speak()
Output:
Meow
polymorphism
A subclass can override the methods it inherits from its superclass. We simply set the same name method on the subclass.
class Feline:
def speak(self):
print("Meow")
class Cat(Feline):
def lick(self):
print("Licking paw")
class Lion(Feline):
def prey(self):
print("Pounces on prey")
def speak(self):
print("ROAR!")
cat = Cat()
cat.speak()
lion = Lion()
lion.speak()
Output:
Meow
ROAR!