SEF9 - 41 Inheritance in Object Oriented (OO) software design

(B) INHERITANCE in Object Oriented Software Design
==========================================================

(1) Inheritance is another concept in Object-Oriented Programming. It involves a class (parent) and a sub-class (child). The parent class is sometimes called the super-class.

(2) The basic idea for inheritance, is to have the attributes and methods of the parent class "common" and "present" in all the children classes. That is how humans inherit, for example, the physical traits of their parents, including physical looks and of course also inherit properties or wealth.

(3) Think of making easy the lives of human dependents (children). For software, think of the children not having to repeat writing the same codes that are already present in the parent. Also for software, think of economizing codes in the children because the more codes we have the more susceptible we are to making errors.

(4) In inheritance, the idea is to make (design) the super-class (parent) as general as possible general, and make (design) the sub-class (child) specialized or specific (sort of, the child class is more detailed in both attributes and methods than its parent class). It is explained in Item(5) below.

(5) Recall that the parent class (person) that we have created earlier (Part A) does not have the attribute "sex", that is, for male and female. So we create the attribute "sex" here in this child class named "personsex". Since "personsex" class inherits everything from its parent (person) class, when we instantiate "personsex" class, we will automatically by inheritance get all the attributes and methods of the parent (person) class. In addition, because we create the "sex" attribute, we get an additional attribute "sex" in the child class, over and above those attributes already present in the parent (person) class.

Except for this new "sex" attribute we have to create, we do not have to re-write the codes for the rest of the attributes and methods that are already in the parent (person) class, and bring them into our child (personsex) class. No need, because by inheritance, the "bringing" is automatic, and we can just use those attributes and methods directly in the child class.

(6) In this particular child class (personsex), i.e. this file, we also created the pair of set-get methods for the attribute "sex" in a similar manner to the way we did in the parent class (Part A). We need these set-get methods so that we can set and get values for the "sex" attribute, the usual thing we need to do.

(7) In addition to writing the new "sex" attribute, in this child class, we have also "rewritten" the method named "farewell(self, message)". Remember that the method "farewell(self, message)" is already present in the parent class (person). You may ask, why should we create this again because we already inherit it? The answer is about "specialization" or "making specific".

(8) In this child class, we actually made a modification to how the "farewell" method work, change its behaviour, so they say. The new farewell(self, message) method here (in the child) works differently from the farewell(self, message) method in the parent class. Even though their calling names and calling parameters are the same, they execute differently (different behaviours) and produce different results. You can see it below in the code in this file and compare to the one in the parent. This effect of "specialized duplication" is called "method overiding" in Object Oriented Programming (OOP) terminology. We do not delete the "farewell" method in the parent class, we just override it with the one we created in the child class.

(9) Can you think why this "overiding" can be done and was done? ANSWER: Think about the methods "drive" for an aircraft and an automobile. We can have the same call method name and same parameter passed like "drive(self, drivespeed)" for both cases but we want the driving actions to be implemented differently in software between an aircraft and an automobile. So if we design the object codes correctly, we can have the software differentiate, and behave differently for the two methods, for example, between "aircraft.drive(self, drivespeed)" and "automobile.drive(self, drivespeed)". Remember the dot notation used to refer to the object name explained in Part 3A.

(10) Also remember, it is not only the "personsex" class here that can inherit the "person" class, we can create other sub-classes that inherit the same "person" class too. Read the sentences in Item(4) above carefully. And the fun part is, sub-classes can also be inherited by sub-sub-classes, and so on. And the lower the classes the more details they have, all because of hierarchical inheritance.

(11) p.s. I am not sure whether there is such a thing as "dis-inheritance" in Object-Oriented Programming (OOP). I know for human beings, some parents "dis-inherit" their sons or daughters for behaving badly.

(12) To run the example for Part B here, you must have the(3) attached files here in the same directory. The command to execute is: "python client2.py". This client2.py program imports only the "personsex.py" class, which in turn imports the "person.py" class. This is a cascading effect.

(13) Can you imagine in a large software engineering project about the number of classes it has, and how the many classes cascadingly call each other? And what happens if you have just one broken link? Ha ha ha. This is where organization comes in, which is SWEBOK Software Configuration Management (one of the five(5) management areas in Software Engineering).

===========================================================

--
WASSALAM
wruslan.hahaha

Attachments:
client2.py
person.py
personsex.py

Full Version with Microsoft Word Version and Attachments
Return to Software Engineering Fundamentals (SEF9MMUWRY)
Previous Topic: 40 Abstraction in Object Oriented (OO) software design
Next Topic: 42 Polymorphism in Object Oriented (OO) software design

0 comments:

Post a Comment