April 25, 2024

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
suman kumar
suman kumar
5 years ago

nice article for beginners.thank you.
welookups

Johng626
Johng626
4 years ago

I’m curious to uncover out what blog system youre employing? Im experiencing some small security troubles with my latest weblog and Id like to locate something more safeguarded. Do you’ve any recommendations? gbfedcfcbgfd

Socopa Corretora
Socopa Corretora
4 years ago

Have you ever thought about adding a little bit more than just
your articles? I mean, what you say is valuable and all.
However think about if you added some great images or videos to
give your posts more, “pop”! Your content is excellent
but with images and video clips, this site could certainly be one
of the most beneficial in its field. Awesome
blog!

Chestnut
Chestnut
4 years ago

Iím not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back later on. Cheers

inheritance-3.blogspot.com
inheritance-3.blogspot.com
4 years ago

Fantastic blog you have here but I was wanting to know if you knew of any community forums that cover the same topics
discussed here? I’d really love to be a part of community
where I can get responses from other experienced individuals that share the
same interest. If you have any suggestions, please let
me know. Kudos!

Bonita
Bonita
3 years ago

Good day! This is kind of off topic but I need some advice from an established blog.

Is it tough to set up your own blog? I’m not very
techincal but I can figure things out pretty
quick. I’m thinking about making my own but I’m not sure where to begin. Do you have any ideas or suggestions?
Many thanks

Innomatics
Innomatics
2 years ago

Become a Data Science expert with us. study Data Science Course in Hyderabad with Innomatics where you get a great experience and better knowledge.

Last edited 2 years ago by Innomatics