Object-oriented Programming (OOP) is a computer programming model that helps one organizes their software design around data, class, and object rather than functions and logic.
Just like you have Knowledge and can perform various methods, Objects have data and can perform various methods.
Class is the blueprint used to create a user-defined data structure. It holds the common attribute and behaviors of the object. For example, the class āRideā will hold objects ācarā, āplaneā and ābikeā.
Object is an instance of a class. It has both a property (variables) and behavior (methods). The python object āCarā will have properties; āmakeā, āmodelā, āfour_wheelsā and behaviors; āstartā, āstopā.
Method is the behavioral factor of the object. Modifying a method in the class āRideā would affect all instances of it; ācarā, āplaneā, and ābikeā.
Instance is simply any object made from the class. An instance of the āRideā class would contain real unique data. A class is like a form while an instance is a filled variation of that form.
It is good practice to identify all the objects you want to manipulate and how they relate among themselves. This is data modeling.
Principles of OOP
1. Encapsulation
State, variables, and methods should be private unless they are being declared public.
An object ācarā of a class āRideā can manage itself via methods āstartā and other class āHouseā cannot touch it expect
allowed to. We can only use the methods to communicate with our object. If we want another class āHouseā to change the state of the ācarā object, we have to create public methods āover_ride_and_startā to modifies or invoke the start
variable. This connection is encapsulation.
2. Abstraction
Hide all implementation of the class from anything outside the class.
Our object should hide details and only expose relevant information to another object. Thus public method should be created so object can be called. The āAl voice recognitionā may start our ācarā object (in the āRideā class) and open the āLightsā object (in the āHomeā class) without having access to the details of our car. This makes maintenance of
an extremely large database less daunting and lowers the chances of bugs.
3. Inheritance
Child class should have a mechanism that enables it to inherit behavior and properties from the parent class.
Reusability is the advantage here. Child class ācarā, āboatā objects should acquire properties (āhas_an_engineā, ācan_be_used_for_transportā) from the parent class āRideā. However, they should have their own unique properties.
4. Polymorphism
Interfacing with objects and receive different forms or results.
This allows us to implement a class exactly as intended for the parent class. This prevents errors in types and each
sub-class keeps its own methods. Our class āRideā has a speed method. Our child class; āObjectā, āboatā and āplaneā can utilize this method. However, they would each have their own iteration of the speed method.
OOP in Python š
Let's Begin! š
Here is a Link to the notebook on my Github. You could download it and follow along.
Class is defined with the class keyword followed by the name of the class.
class Car:
pass #This is also where subsequent code will eventually go.
The pass acts as a place holder and thus prevents python from returning an error. At the moment, our car class is without attributes or methods. Let's instantiate our object class by calling the class.
car()
Every instance of our class is stored in a new memory address. which means every instance of the Car class represents two different objects. Wanna try?
a = Car()
b = Car()
print (a == b)
Our class has a docstring attribute called doc which holds the documentation of the object.
class Car:
#this is the docstring and it gives a brief about the class
""" A four-wheeled road vehicle that is powered by an engine car has four wheels"""
Car.__doc__
Letās make our class interesting by giving it attributes. There are two types; Class attributes and Instance attributes.
Every attribute in our class would be defined in a method called .init() with self as the first parameter. These are called instance attributes. However, class attributes are attributes that have the same value for all instances of that class. Hence it would be assigned outside the .init() method. All instances of car class (model and colour) are four-wheeled.
class Car:
#Class attributes
wheels = "four"
def __init__(self, model, colour):
self.model = model
self.colour = colour
Letās give our instance attributes parameters values. š§Ø
Toyota = Car("Toyota", "yellow")
kia = Car ("Kia", "black")
Ford = Car("ford", "blue")
Honda = Car("Honda", "brown")
We have four Car instances with each having unique values. Letās access the data in these attributes.
#instance attributes
Toyota.model
print(Toyota.model)
kia.colour
print(kia.colour)
#class attributes
Toyota.wheels
print(Toyota.wheels)
kia.wheels
print (kia.wheels)
Like I mentioned all instances in a class have .wheels, .model and .colour. However, the values of the instances attributes (.model and colour) would differ; encapsulation and polymorphism. We can also update and modify the values in our instance without changing the values.
#modifying our attributes
Toyota.colour = "red" #previously yellow
print (Toyota.colour)
kia.wheels = "two" #previously four
print (kia.wheels)
It worked!! š So objects are mutable.
Letās look at instance methods. Just like .init(), the first parameter is also always self and is defined in our class. Letās create two instance methods; speed and details.
class Car:
wheels = "four"
def __init__(self, model, colour):
self.model = model
self.colour = colour
# Instance method
def details(self):
return f"this {self.model} is {self.colour}"
# Another instance method
def speed(self, speed):
return f"This {self.model} is {speed} at top speed"
Let's apply these new methods.
Toyota = Car("Toyota","yellow")
print (Toyota.details())
#Toyota.speed(280)
print (Toyota.speed(280))
Letās look at inheritance. Letās say we want to add a new class āengineā. We canāt always specific the engine for each car. Letās create a child class for it
class Hybrid_engine(Car):
pass
class Gas_engine(Car):
pass
Now letās instantiate these engines for the cars.
Toyota = Gas_engine("Toyota", "yellow")
kia = Gas_engine("kia", "black")
Honda = Gas_engine("Honda", "brown")
Ford = Hybrid_engine("Ford", "blue")
Letās try some basic functions.
print(type(Toyota))
print(isinstance(Toyota, Car))
print(type(Ford))
print (isinstance (Ford, Gas_engine))
#Toyota, Kia, Honda and Ford are all instance of Car
#but Ford wouldnāt be in the Hypdrid engine child class instance.
Thus, all objects created from the child class is an instance of the parent (car) but might not be in the same child class (Gas_engine and Hybrid_engine). Letās give it the parentās functionality specific to it (Inheritance)
class Hybrid_engine(Car):
def speed(self, speed="210"):
return f"{self.model} Speed; {speed}"
Ford = Hybrid_engine("Ford", "blue")
print (Ford.speed())
A change in the parent class wouldnāt affect the child class.
class Car:
wheels = "four"
def __init__(self, model, colour):
self.model = model
self.colour = colour
# Instance method
def details(self):
return f"this {self.model} is {self.colour}"
# Another instance method
def speed(self, speed):
return f"This {self.model} is {speed} at top speed"
Toyota = Gas_engine("Toyota","yellow")
print (Toyota.speed(367))
#however ford wouldnāt get the change since we define his child class
Ford = Hybrid_engine ("Ford", "blue")
print (Ford.speed(467))
However, if we didnāt want this to happen. We would need to use the .super built-in function for the child class. š
class Hybrid_engine (Car):
def speed(self, speed="210"):
return super().speed(speed)
Ford = Hybrid_engine ("Ford", "blue")
print (Ford.speed())
Summary š
In this article, we discussed Object-oriented Programming and the principles which govern it.
Then we looked at how to define a class, how to instantiate an object from a class, and giving this class some sought of documentation using .doc. Let's not forget we looked at an instance and class attributes and defining properties and behaviors of an object. We also discussed some function such as type(), isinstance() and super(). š
I hope you found this notebook helpful. Remember to share on your favorite social media so other folks can find it, too. Thanks š