DEV Community

John Wakaba
John Wakaba

Posted on

OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON SUMMARY

OOP can be broken down into several key concepts

  • Object
  • Class
  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

Object

  • Everything can be treated as an object
  • Objects are defined using two things
  1. Properties: Describe an object
  2. Behavior: Functions that an object can undertake or perform
  • A car object for instance
  • Properties: Color, Model, Make, Year, Color
  • Behavior: Accelerating, Braking, Turning

One may ask how are objects created this is where classes come into play

Class

  • A class is a blueprint for creating objects
  • A class defines a set of properties(attributes) & functions (methods) that will be common to all objects created from the class.
  • A class can have many objects
  • A class is a blueprint
  • Object is an instance of that class
  • Methods define behaviors
  • Attribuites define characteristics
  • Self is a key attribute
  • Self is a placeholder, keyword where the object is placed

I will start with a simple example: A class Individual with the attributes name, age, address, E-mail.

class Individual:
    def __init__(self,name,age,address,email):
        self.name = name
        self.age = age
        self.address = address
        self.email = email
    def get_details(self):
        print(f' {self.name} of age {self.age} who resides at {self.address} and email {self.email} is our best Analyst')
Enter fullscreen mode Exit fullscreen mode
I_1 = Individual('Raymond', 44, '121 Bolleavue', '[email protected]')
I_1.get_details()
Enter fullscreen mode Exit fullscreen mode

Output : Raymond of age 44 who resides at 121 Bolleavue and email [email protected] is our best Analyst

Inheritance
Inheritance helps us to inherit all the properties/methods of parent class & add new ones or override existing.

We want to create a student and do not want to define the attributes and methods of the basic Individual class.

class Student(Individual):
    def __init__(self, name, age, address, email, cohort):
        super().__init__(name, age, address, email)
        self.cohort = cohort
    def enroll_course(self):
        print(f'Enroll {self.name} of age {self.age} and address {self.address} and email {self.email} into cohort {self.cohort}')
Enter fullscreen mode Exit fullscreen mode
my_student = Student('Arnold',30,'180 Nalow','[email protected]','C03')
my_student.enroll_course()
Enter fullscreen mode Exit fullscreen mode

Output : Enroll Arnold of age 30 and address 180 Nalow and email [email protected] into cohort C03

Encapsulation

Encapsulation involves keeping data safe inside a class
Restricting direct access to certain attributes and methods.
We use private attributes/methods (with a _ or __ prefix) to achieve this.

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.__speed = 0

    def increase_speed(self,speed):
        #call the private __accelerate method to increase speed
        self.__accelerate(speed)

    def get_Speed(self):
        return self.__speed

    def __accelerate(self, speed):
        # Ensures that speed is not increased beyond a certain limit.
        if self.__speed + speed <= 100:
            self.__speed += speed
        else:
            self.__speed = 100
Enter fullscreen mode Exit fullscreen mode
car_1 = Car('Toyota','Mitsubishi',2006)
car_1.get_Speed()
Enter fullscreen mode Exit fullscreen mode

Output : 0

car_1.increase_speed(60)
car_1.get_Speed()
Enter fullscreen mode Exit fullscreen mode

Output : 60

car_1.increase_speed(130)
car_1.get_Speed()
# Outputs 100 because it does not go beyond
# The behavior of speed <= 100 is privately encapsulated.
Enter fullscreen mode Exit fullscreen mode

Abstraction

Focuses on exposing only essential information to the outside world while hiding implementation details.

Shows simplicity for the user and hides print logic

Polymorphism

In polymorphism behavior changes based on the object,
allows us to use a single interface for different data types or classes.

It is achieved through a method called overriding
whereby a subclass provides a different implementation for a method defined in its parent class.

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year


    def honk(self):
        print('Roar roar!!')


class FormulaCar(Car):
    def honk(self):
        print('Rev rev!!')


class Lorry(Car):
    def honk(self):
        print('Rumb rumb!!')

Enter fullscreen mode Exit fullscreen mode
simple_car = Car('Toyota','Vitz','2016')
formula_car = FormulaCar('Mercedes','W11','2020')
lorry_car = Lorry('Mitsubishi','Canter',2007)
vehicles = [simple_car, formula_car, lorry_car]
for vehicle in vehicles:
    vehicle.honk()
Enter fullscreen mode Exit fullscreen mode

Output
Roar roar!!
Rev rev!!
Rumb rumb!!

Why Polymorphism?

  1. Enables one define standard interface for similar classes
  2. Reusability of Code: There is no need to define init over an over
  3. There is flexibility to over ride the parent class methods on need basis

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

close