Mastering Python OOP: All in One Example
Unlock the full potential of Python's Object-Oriented Programming (OOP) with a deep dive into key concepts. Explore the power of encapsulation, inheritance, polymorphism, and abstraction through professional examples. Elevate your coding skills and design cleaner, modular, and efficient Python applications.
from abc import ABC, abstractmethod
# Abstract class for Employee
class Employee(ABC):
def __init__(self, name, salary):
self.name = name
self.salary = salary
@abstractmethod
def calculate_bonus(self):
pass
# Concrete class representing a Developer
class Developer(Employee):
def __init__(self, name, salary, programming_language):
super().__init__(name, salary)
self.programming_language = programming_language
def calculate_bonus(self):
return self.salary * 0.1
# Concrete class representing a Manager
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def calculate_bonus(self):
return self.salary * 0.15 + self.team_size * 1000
# Concrete class representing a Company
class Company:
def __init__(self, name):
self.name = name
self.employees = []
def hire_employee(self, employee):
self.employees.append(employee)
def display_employee_info(self):
for employee in self.employees:
print(f"{employee.name} - Salary: ${employee.salary}, Bonus: ${employee.calculate_bonus()}")
# Example Usage
if __name__ == "__main__":
# Creating instances of employees
dev1 = Developer("Tahsin Soyak", 80000, "Python")
manager1 = Manager("Bilge Özcan", 100000, 5)
# Creating a company
company = Company("Tech Innovators")
# Hiring employees
company.hire_employee(dev1)
company.hire_employee(manager1)
# Displaying employee information
company.display_employee_info()
Here’s a breakdown of the context:
Abstract Class (Employee):
The Employee _ class is defined as an abstract class. Abstract classes serve as blueprints for other classes and contain at least one abstract method. In this example, _calculate_bonus() is the abstract method. All subclasses must implement this method.
The Employee class includes common attributes and methods for all types of employees.
Concrete Class (Developer):
The Developer class is a concrete subclass of Employee .
It includes additional attributes, such as programming_language.
It implements the abstract method calculate_bonus() specific to developers. In this example, the bonus is calculated as 10% of the salary.
Concrete Class (Manager):
The Manager class is another concrete subclass of Employee.
It includes additional attributes, such as team_size .
It implements the abstract method calculate_bonus() specific to managers. In this example, the bonus is calculated based on both the salary and the size of the team managed.
Company Class:
The Company class represents a company that can hire employees.
It has methods to hire employees and display information about all employees.
Example Usage:
Two employees are created: one Developer and one Manager .
A Company instance is created, named Tech Innovators .
Employees are hired by the company.
Information about the employees is displayed, including their name, salary, and calculated bonus.
The example demonstrates the following OOP concepts:
Encapsulation: Attributes are encapsulated within classes.
Inheritance: Subclasses are created from a base class.
Polymorphism: Different classes implement the same method in different ways.
Abstraction: An abstract class is used to define a blueprint, ensuring methods are implemented by subclasses.
If you need further information or have specific questions, feel free to ask! Tahsin Soyak tahsinsoyakk@gmail.com
Top comments (3)
A decent example but you should explain what's going on.
I changed the breakdown of code. Now its focus on example usage more instead of just terms of OOP. Thanks for feedback.
The example provides a simplified model of a company's employee structure, with different types of employees (Developers and Managers) and a class representing the company itself.