Object Oriented Programming With Python

In this lesson, we would cover the following:

  1. Object Oriented Programming Terms
  2. Creating a Class in Python
  3. Inheritance: Creating a Subclass in Python
  4. Instantiating a Class

 

 

1. Object Oriented Programming Terms

  • Class: A class is a user-defined type that could be used to model object in real world. A class defines the attributes and methods of an object. It serves as the blueprint from which objects are created
  • Class Variable: This is a variable that belongs to the class. This variable is used by all the objects created from that class
  • Instance Variable: This is a variable that belongs to one instance of the class. It belongs to the object created from the class and so is also called object variable
  • Inheritance: A feature that allows a class to inherit the features (variables and methods) of another class
  • Overloading: A feature that allows two or more functions to have the same name but behave differently depending on the parameters.
  • Instantiation: Creating an object from a class
  • Derived Class (Sub-class or child class): A class that inherits from another class.

 

2. Creating a Class in Python

We create a class in python using the class keyword. Inside the class, we may define a constructor using the def __init__ syntax.
An example of a class definition to model an Employee is given in Listing 1.0.

# The employee class in python
class Employee:
    employeeCount = 0
# This is a constructor (initializer)
def __init__(self, firstname, lastname, department, salary):
self.firstname = firstname
self.lastname = lastname
self.department = department
self.salary = salary
def display(self):
print("Name: " + self.firstname + ", " + self.lastname)

Listing 1.0: Employee class in Python

 

3. Inheritance: Creating a Subclass

Let’s say in a company there are different categories of employees. For example Driver. A driver is also an employee and has all the attributes of an employee.
To create a driver class we can simply inherit the features from the employee class by creating a sub class(derived class or child class). In python, this is achieved by placing the parent class inside parenthesis
How to do this is given in Listing 1.1

class Driver(Employee):
    car = " "
def display(self):
print("Name: " + self.firstname + ", " + self.lastname)
print("Assigned car: " + self.car)

Listing 1.1: Driver class derives from Employee class

 

Notice that listing 1.1 also includes a definition of the display method exactly with the same name as in the Employee class. This is called method overriding. This is because the method in the subclass overrides the method in the superclass.

 

4. Instantiating a Class

To instantiate a class(create an object of a class), we simply assign the name of the class to a variable and provided the necessary attributes of the object.
In Listing 1.2 we instantiate both the Driver and the Employee class

employee1 = Employee("Osondu", "Munonye","Admin","5000")
employee2 = Employee("Adaku", "Okeke", "Human Resource", 5400)
driver1 = Employee("Jackson", "Bills","Assets",4400)
driver2 = Employee("Okereke", "Joseph", "Maintenance", 3300)

Listing 1.2: Instantiating classes

 

In the next tutorials, we would examine how to access attributes of a class

Object Oriented Programming – Video Lessons
How to Create a Class in Python
Instantiating a Class in Python
Accessing Attributes of Objects
Inbuilt Attribute and How to Create Subclasses