Inheritance, Types of Inheritance

 Inheritance - Main aims

i) Reusability
ii) Maintenance
iii) Code Extension 

Let us say, we have a class name Teacher. Then we are describing teacher using Teacher class object. 
Assume we want to add some special teachers to improve quality of education with special privilege than normal teacher. Then it makes perfect sense to create a special teacher class which have characteristics of teacher with some additional privilege which can be achieved by extending teacher. In object-oriented terms this mean inheritance. 
class Teacher:
    #init method with three attributes
    def __init__(self,name,staffid,subject):
        self.name = name
        self.subject= subject
        self.staffid = staffid
    #adding extra attribute
    def setDesignation(self,designation):
        self.designation = designation
    #displaying required attribute
    def showDetails(self):
        print("Name ",self.name,"Subject : ",self.subject,"Designation ",self.designation)

class SpecialTeacher(Teacher):
    #setting special attribute for Teacher class only
    def setSpeciality(self,spec):
        self.setSpeciality = spec
    #defining new init method with extra spec
    # argument type and list must match with Teacher class
    def __init__(self,name,staffid,subject,spec):
        self.spec = spec
        super().__init__(name,staffid,subject)   
s1 = SpecialTeacher("Hari",101,"maths","stats")  #s1.__init__(s1,"Hari",101,
                                                 #"maths","stats")
s1.setDesignation("Engineer")
s1.showDetails()  

# s1 object will have
# Both the methods from Teacher class
# ( show Details and showDesignation)
#As well as extra method setSpeciality
#We can check using dict object

print(s1.__dict__)
#object contents
'''
{'spec': 'stats', 'name': 'Hari', 'subject': 'maths',
'staffid': 101, 'designation': 'Engineer'}
'''
for k,v in SpecialTeacher.__dict__.items():
    print(k,v)
#SpecialTeacher Class
'''
__module__ __main__
setSpeciality <function SpecialTeacher.setSpeciality at 0x0000025054659260>
__init__ <function SpecialTeacher.__init__ at 0x0000025054659300>
__doc__ None
'''


Point to be noted. 

i) In python there is no compulsion to define __init__ method. If it is not defined than its not there. Only purpose of this method is to initialize objects upon creation. 

ii) If we define __init__ in parent class and same in child to initialize child object, then argument should match with parent as by default it is inherited to child. 

In above case three are 3 arguments in Parent class, so in Child class 3 positional arguments are required at least. 

iii) Updating class members / Object. 

Let us say we want to add new attribute specialist in SpecialTeacher class. Then either we can add that new attribute to Teacher class, but by doing so we are changing the meaning of Teacher class. This would be unnecessary, and significance of class is dismantled. In order to solve this, we can do following
  • Create new class and do inheritance by extending existing functionality
  • If we want to initialize Child class along with attribute of Parent that all the members should be initialized 
 # argument type and list must match with Teacher class
    def __init__(self,spec,name,staffid,subject):
        self.spec = spec
        self.name = name
        self.staffid= staffid
        self.subject = subject

    Since, we are overriding attribute by manually initalizing, it would be appropriate to call parent class init

    # argument type and list must match with Teacher class
    def __init__(self,spec,name,staffid,subject):
        self.spec = spec
        super().__init__(name,staffid,subject)

OR 

def __init__(self,spec,name,staffid,subject):
        self.spec = spec
        Teacher.__init__(self,name,staffid,subject)

Merely writing Teacher means we are calling class object Teacher. Since we are calling it with class object and accessing members, self has to be passed (like earlier Test class has to pass object reference explicitly. Here, also it would be needed) and remaining attribute from Parent.  But in case of super() this is handled by compiler and we don't explicitly need to pass the object implicitly. 

Type of Inheritance

'''
i) Single Inheritance
ii) Multiple Inheritance
iii) Multilevel Inheritance
iv) Hierarchical Inheritance
v) Hybrid Inheritance
'''






Comments