Class inheritance

Class Inheritance in Python

While coding an application a developer will create several classes. Sometimes, he may feel the need to create a class similar to an existing one but with some additional features. In such a case instead of creating a class from scratch he can create a class that inherits the existing class and then add the new functionality to it.

Let’s try to explain this with a simple example. Suppose we have a class called human which has some features. Then we try to create two classes – boy and girl. Now, both boy and girl are humans but different from each other. So, they both can inherit the common properties of human class and then define their own features that separate them from each other.

class human:
     def __init__(self):
         print ("Human class is the parent class")
     def human_method(self):
         print ("I am a parent method being called from the child object of class girl")

class girl(human):
     def __init__(self):
         print ("girl class is inherited from human")
     def girl_method(self):
         print ("I am a child method being called from the child object of class girl")
         
    
g  = girl()
g.girl_method()
g.human_method()

Download the code Run the code

In the above code have a look at how the girl class inherits the human class – “class girl(human):
” . You should also notice that an object of the girl class can call the method of the human class.

Similarly, a class can have more than one parent

class woman:
    class_definition

class man:
    class_definition

class baby(woman,man):
    class_definition

issubclass() function

issubclass() is a function that can be used to find whether a class is a subclass of another given class or not. If yes, it returns true else it will return false.

 

class woman:
    pass

class man:
    pass

class baby(woman,man):
    pass


if issubclass(baby,man):
    print("baby is a subclass of man")
if issubclass(baby,woman):
    print("baby is a subclass of woman")

Download the code Run the code

Overriding methods

Child class can override any method of the parent class. If you have a method defined in a parent class and you want it to work differently in any of its child class then you can define the new functionality for the same method in the child class. Have a look at the code below how the method hi_whomsoever() is defined differently in the parent and the child class.

class human:
     def __init__(self):
         print ("Human class is the parent class")
     def human_method(self):
         print ("I am a parent method being called from the child object of class girl")
     def hi_whomsoever(self):
         print("Hi human")

class girl(human):
     def __init__(self):
         print ("girl class is inherited from human")
     def girl_method(self):
         print ("I am a child method being called from the child object of class girl")
     def hi_whomsoever(self):
         print("Hi girl")
    
h  = human()
h.hi_whomsoever()

g  = girl()
g.hi_whomsoever()

Download the code Run the code

<<< Python Classes, Objects and OOP conceptsGenerators >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .