Key components of Object Oriented Programming
-
Class - It is a logical codeblock that contains Attributes and Behaviour. A class is defined with the ‘class’ keyword. The Attributes can be variables, and the Behaviour can be functions. A class provides a blueprint for an Object. For instance, you want to record the attributes of employees at a firm, such as their position and employment status You can create a class called employee, and conveniently bundle those attributes in one place.
-
Objects - Instances created from a class is called an Object. You can create an infinite number of objects. The state of an Object comprises its Attributes and Behaviour, and each one has a unique identifier that distinguishes it from other instances. The Attributes and Behaviour of a class is what dictates the state of the object. For instance, the you can create an object of the employee class above, and then define the position and employment status as ‘General Manager’ and ‘Full time’ respectively.
-
Methods - Functions defined inside of a class that determine the behaviour of an object instance. Let’s say you want the employee objects to output a string that states their postiion. You would first define the function inside of the employee class, and then call it on an object to get the outputs.
Creating a class:
class MyClass: a = 5 # attribute
def hello(self): print("Hello World")
myc = MyClass() # an object for the above class
print(MyClass.a) # class referenceprint(myc.a) # object referenceprint(myc.hello())Another example below:
class House: num_rooms = 5 bathrooms = 3
def cost_evaluation(self): print(self.num_rooms) pass # functionality to calculate the costs from the area of the house
house = House()print(house.num_rooms)print(house.num_rooms)
# modifying instance attributehouse.num_rooms = 7print(house.num_rooms)print(house.num_rooms)
# modifying class attributeHouse.num_rooms = 7print(house.num_rooms)print(house.num_rooms)Four Concepts of Object Oriented Programming
1. Inheritance - Creating a new class which is a derivative of an existing one. The original is a parent or super class, while the derivative is called a child or sub class. Below is a simple template for inheritance:
class Parent: # Members of the parent class pass
class Child(Parent): # Inherited members from parent class # Additional members of the child class pass2. Encapsulation - It limits access to methods and variables by encasing them in a single unit of scope. It helps prevents unwanted modifications therefore reducing errors in output.
class Alpha: def __init__(self): self._a = 2. # Protected member of 'a' self.__b = 2. # Private member of 'b'3. Polymorphism - The ability of a function to change its behaviour when called by different objects. Everything in python is an object. It could be an operator, method or any object of some class.
string = "poly"num = 7sequence = [1, 2, 3]new_str = string * 3new_num = num * 3new_sequence = sequence * 3The outputs of the above operators are different. That is polymorphism.
4. Abstraction - The ability to hide implementation details for data security i.e, to make data safer and more secure. Python does not support abstraction directly but uses inheritance to achieve it.
from abc import ABC
class ClassName(ABC): pass