Object-oriented programming is a concept that you will probably find in the most modern languages such as Python & Java. The notion is to break your code into pieces using the concept of the object. This makes things easier for the development team inside the company.
In the last tutorial about object-oriented, we’ve discussed using classes, adding objects to the classes, attributes, and more. If you didn’t read it, I highly recommend checking it out and returning to this tutorial.
1. Inheritance
In some cases, when you write a python code, you need to use many classes inside this program. These classes can have the same attribute, so you can inherit them from another class in your code instead of rewriting these attributes.
It is called inheritance, and it makes you inherit attributes or functions (Child Class) from another class (Parent Class). Let me show you an example to understand:
#The Parent Class
class Organization_Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
#The Child Class
class Manager(Organization_Employee):
pass
#The Child Class
class Database_Admin(Organization_Employee):
pass
#Creating the objects (Instances)
ma = Manager("John", 5700)
da = Database_Admin("Juan", 9700)
#Priting the objects informations
print(ma.name, ma.salary)
print(da.name, da.salary)
Output:
John 5700
Juan 9700
We start first the code by defining the parent class, which will other classes the child class inherit from it:
class Organization_Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
When you later create the chile class, you need to pass the name of the parent class inside the closed brackets of the chile class to inherit its attributes from which is the Organization_Employee class:
class Manager(Organization_Employee):
pass
class Database_Admin(Organization_Employee):
pass
Later, you will create instances as you learned before in the previous article about object-oriented programming:
ma = Manager("John", 5700)
da = Database_Admin("Juan", 9700)
2. Function Inheritance
You can print these values if you want or use them in your code. I’ve mentioned that you can inherit the attributes from the parent class, but you can inherit the functions from the parent class to the child class. Let me show you an example:
#The Parent Class
class Organization_Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
#The work function
def work(self):
print("{} is working in the company as a full-time job".format(self.name))
#The Child Class
class Manager(Organization_Employee):
pass
#The Child Class
class Database_Admin(Organization_Employee):
pass
#Creating the objects (Instances)
ma = Manager("John", 5700)
da = Database_Admin("Juan", 9700)
#Calling the work function
ma.work()
da.work()
Output:
John is working in the company as a full-time job
Juan is working in the company as a full-time job
3. Child Class Attributes
When you create the parent class and add some functions to this class, it will by default transfer these functions to the child class when you perform inheritance without making any changes to the code.
These previous examples show you that the child class will inherit everything from the parent class, such as the attributes and functions, but what if we want the child class to have also its attributes to use?
The good thing is that you can make every child class has other attributes other than the same attributes of the parent class. Let me show you an example:
#The Parent Class
class Organization_Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
#The work function
def work(self):
print("{} is working in the company as a full-time job".format(self.name))
#The Child Class
class Manager(Organization_Employee):
def __init__(self, name, salary, age, country):
super().__init__(name, salary)
self.age = age
self.country = country
#The Child Class
class Database_Admin(Organization_Employee):
pass
#Creating the objects (Instances)
ma = Manager("John", 5700, 33, "Canada")
da = Database_Admin("Juan", 9700)
print(ma.age, ma.country)
Output:
33 Canada
We’ve changed a few things in the Manager child class. So you can see that we added the __init__ function with the parent attributes and some other attributes, age, and country.
def __init__(self, name, salary, age, country):
We use the supper() function to tell python that these two other attributes are inherited from the parent class. This function is used to extend the characteristics of the child class:
super().__init__(name, salary)
Then, you store the newly added attributes of the child class in the child class using the self keyword:
self.age = age
self.country = country
When you access these attributes from the object you’ve created from that child class, it will print them, as you can see since we’ve accessed them from the manager objects. If you try the same, but from the database_admin object, you probably get an error.
4. Polymorphism
Polymorphism lets you specify a function in the child class that has the same name as its parent class, but every child class will keep its functions as they are. Let me show you an example:
#The Parent Class
class Organization_Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
#The work function
def work(self):
print("{} is working in the company as a full-time job".format(self.name))
#The Child Class
class Manager(Organization_Employee):
def __init__(self, name, salary, age, country):
super().__init__(name, salary)
self.age = age
self.country = country
#The work function:
def work(self):
print("{} is managing the team in the organization".format(self.name))
#The Child Class
class Database_Admin(Organization_Employee):
#The work function:
def work(self):
print("{} designing the database for the organization".format(self.name))
#Creating the objects (Instances)
Organization_Employee = [Manager("John", 5700, 33, "Canada"),
Manager("Canelo", 8700, 43, "Germany"),
Database_Admin("Juan", 9700),
Database_Admin("Dustin", 9200)]
def Promotions(Organization_Employee):
for employee in Organization_Employee:
employee.work()
Promotions(Organization_Employee)
Output:
John is managing the team in the organization
Canelo is managing the team in the organization
Juan designing the database for the organization
Dustin designing the database for the organization
You can see that every child class, the Manager & Database_admin, has the same name function, the work() function. Then, we’ve created a list of employees to apply this concept to them:
Organization_Employee = [Manager("John", 5700, 33, "Canada"),
Manager("Canelo", 8700, 43, "Germany"),
Database_Admin("Juan", 9700),
Database_Admin("Dustin", 9200)]
Next, we will create a function called Promotions that will iterate through these employees and apply the polymorphism concept to them:
def Promotions(Organization_Employee):
for employee in Organization_Employee:
employee.work()
You can now call the function and apply it to the employees like in the previous example:
Promotions(Organization_Employee)
The output shows that it used the work() function of every child class instead of the parent class, so it has overridden the parent class function.
Conclusion
Object-oriented programming is an essential skill you need to become a good developer and work in a team since it is developed to make building the software inside the company much more straightforward.