Saturday 26 December 2020

Part Four: Conclusion to Object Oriented Programming (OOP)

Congratulations on completing Part Four, it is a bit more of an advanced topic and it is the final part of our course. You have learned a lot and on your way to becoming a programmer with Python 3.

Here is a summary of what was learned in Part Four:

·        We have learned about types (objects) and classes in Python 3.

·        We have learned that we have been working with in-built types throughout the course.

·        We have also learned how to create our own user-defined classes in Python 3.

·        We created the Person class and used the __init__ method.

·        We learned that we can add as many methods (functions) under our user-defined classes.

·        We created the Mammal class and using the concept of class inheritance created subclasses Dog class and Cat class.

·        We created the Employee Payment Platform System (EPPS) using Object Oriented Programming (classes).

Wednesday 9 December 2020

Part Four: Example 4: Employee Payment Platform System (EPPS)

 In this example, we are going to create a very basic template of a government employee payment system that would be used to pay three different kinds of workers. The classes we would consider in this example are mainstream government workers, university workers and oil workers.


The first class to be created would be that of the mainstream workers because it contains all the attributes common to all the three classes of workers. 
Next, we create the university class of workers which would inherit all the attributes of the mainstream workers and in addition would have the attribute ‘allowance’ which is peculiar to only this class of workers. 
Finally, we would create the oil worker class which would also inherit all attributes from the mainstream worker class but would have the peculiar attribute ‘offshore’ attributed to this class of workers only.
 
Below is the code to execute the Employee Payment Platform System (EPPS). 

 

print('Employee Payment Platform System (EPPS)')
print('------------------------------------------')


class Employee:
    def __init__(self, name, age, rank):
       self.name = name
       self.age = age
       self.rank = rank

    def pay(self):
        print('pay')


class University(Employee):
    def allowance(self):
        print('allowance')


class Oil(Employee):
    def offshore(self):
        print('offshore')


print('')
employee1 = Employee('Mike', 34, 'Manager')
employee1.pay()

print('')
print('--------------------')
university1 = University('Ali', 28, 'Dean')
university1.pay()
university1.allowance()

print('')
print('--------------------')
oil1 = Oil('Jay', 45, 'Supervisor')
oil1.pay()
oil1.offshore()

 

When we run the code we have the following output:

 

Employee Payment Platform System (EPPS)

------------------------------------------

 

pay

 

--------------------

pay

allowance

 

--------------------

pay

offshore

 

 From the code above, all employees have common parameters (name, age, rank) and a common method (function) ‘pay’.

However, the university class inherits all those attributes from the employee class and in addition have the peculiar method, ‘allowance’ attributed to this class only.

Similarly, the oil worker class inherits all the attributes from the employee class and in addition have the peculiar attribute ‘offshore’.

We can then add our employees based on the class they belong and they would automatically get all the payment they are entitled to.

If required, at any time, we can add, remove, modify any of the attributes in any of the classes and the changes would affect only the targeted employees.

 

 

 

 

 

Monday 7 December 2020

Part Four: Example 3: class Inheritance

 We can create a class that would have all the common attributes then create subclasses that would have peculiar attributes in addition to the common attributes in the main class.

Example is the class Mammal below. The main class has the attribute walk which is common to the Mammal class. 
However, in the subclass Dog there is the peculiar attribute bark in addition to the common attribute walk.
Also, in the subclass Cat there is the peculiar attribute meow in addition to the common attribute walk.
 
The code is given below:
 
 

class Mammal:
   def walk(self):
      print('walk')

class Dog(Mammal):
   def bark(self):
      print('bark')

class Cat(Mammal):
   def meow(self):
      print('meowing')

dog1 = Dog()
dog1.walk()

cat1 = Cat()
cat1.meow()

When we run the code we have the following output:

 
walk
meowing 

Part Four: Example 2: class Person

 In this example, we would add some more methods (functions) to our person class. We would add the methods to walk and speak such that it can be invoked when we run the code as shown below.

 

class Person:
   def __init__(self, name, age):
      self.name = name
      self.age = age

   def walk(self):
      print(self.name + ' is walking...')

   def speak(self):
      print('Hello my name is ' + self.name + ' and I am ' + str(self.age) + ' years old')

ali = Person('Ali', 22)
jay = Person('Jay', 18)

print(ali.name + ' ' + str(ali.age))
ali.speak()
ali.walk()

print(jay.name + ' ' + str(jay.age))
jay.speak()
jay.walk()

 

When we run the code we have the following output:

 

Ali 22

Hello my name is Ali and I am 22 years old

Ali is walking...

Jay 18

Hello my name is Jay and I am 18 years old

Jay is walking...