python inheritance

By | March 13, 2019

Python Inheritance-: Inheritance is an important feature of the object-oriented programming. Inheritance provides code reuse ability to the program because we can use an existing class to create a new class instead of creating it from scratch. For a example there are many birds and  every bird has a special species.

In another words we can say that the inheritance is the process for creating a sub class as like Main class ,child class and many more child classes. These are the part of main class.

In inheritance the child class acquires the properties and can access all the data members and functions defined in the parent class. A child class can also provide its specific implementation to the functions of the parent class.

In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. Example of inheritance

class Animal:  
    def speak(self):  
        print("Animal roaring")  
#child class lion inherits the base class Animal  
class lion(Animal):  
    def bark(self):  
        print("lion roaring")  
d = lion()  
d.bark()  
d.speak()  

Output of this 
Animal roaring
lion roaring

Multilevel inheritance in python -: Python also support multilevel inheritance.as like

class Animal:  
    def speak(self):  
        print("Animal roaring")  
#The child class lion inherits the base class Animal  
class lion(Animal):  
    def bark(self):  
        print("Lion roaring")  
#The child class lionchild inherits another child class lion  
class lionchild(lion):  
    def eat(self):  
        print("Eating meat")  
d = DogChild()  
d.bark()  
d.speak()  
d.eat()  


Output of this programme

lion roaring
Animal roaring
Eating meat

 

Inheritance with class and Instance variable-: Inheritance allows an instance to inherit attributes of the class. Instance variables are associated with object instances and only one instance variable of a given name exists for a given instance. As like..

class P:
    z = "Hello"
    def set_p(self):
        self.x = "Class P"
    def print_p(self):
         print(self.x)
class C(P):
    def set_c(self):
        self.x = "Class C"
    def print_c(self):
        print(self.x)

When we execute this code..

>>> c = C()
>>> c.set_p()
>>> c.print_p()
Class P
>>> c.print_c()
Class P
>>> c.set_c()
>>> c.print_c()
Class C
>>> c.print_p()
Class C

 

The object c in this example is an instance of class C. C inherits from P but c does not inherit from some invisible instance of class P. It inherits methods and class variables directly from P. Because there are only one instance (c), any reference to the instance variable x in a method invocation on c must refer to c.x. This is true regardless of which class defines the method being invoked on c. As we can see  when they are invoked on c both set_p and print_p, defined in class P and refer to the same variable which is referred to by set_c and print_c when they’re invoked on c.

Class variables are inherited but we should take care to avoid name clashes and be aware of a generalization of the behavior we saw in the subsection on class variables. In this example a class variable z is defined for the superclass P and can be accessed in three ways, through the instance c, through the derived class C, or directly through the superclass P

>>> c.z; C.z; P.z
'Hello'
'Hello'
'Hello'

 

If we try setting the class variable z through the class C, a new class variable is created for the class C. This result has no effect on P’s class variable itself . But future accesses through the class C or its instance c will see this new variable rather than the original as like

>> C.z = "netnic"
>>> c.z; C.z; P.z
'netnic'
'netnic'
'Hello'

Similarly, if we try setting z through the instance c, a new instance variable is created, and we end up with three different variables as like

>>> c.z = "Ciao"
>>> c.z; C.z; P.z
'Ciao'
'Bonjour'
'Hello'

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *