Friday, December 27, 2024
Google search engine
HomeLanguagesInheritance in Python | Set 2

Inheritance in Python | Set 2

Prerequisite : basics of inheritance in Python, Inheritance, examples of object, issubclass and super

There are 2 built-in functions in Python that are related to inheritance. They are:

1. isinstance(): It checks the type of an object. Its syntax is:
isinstance(object_name, class_name)
It would return True if the class of object_name is class_name else False.
For example:




# Python code to demonstrate issubclass()
  
print(isinstance(5, int))


Output:

True

The above code would show the following output:
True
This is because 5 is an integer and hence belongs to the class of int.
NOTE: ‘int’ is both a type and a class in Python.

2. issubclass(): It checks whether a specific class is the child class of another class or not. Its syntax is:
issubclass(childclass_name, parentclass_name)
It would return True if the entered child class is actually derived from the entered parent class else, it returns False.
For example:




# Python code to demonstrate issubclass()
class A():
      def __init__(self, a):
            self.a = a
class B(A):
      def __init__(self, a, b):
            self.b = b
            A.__init__(self, a)
  
print(issubclass(B, A))


Output:

True

The above code would show the following output:
True

Multiple Inheritance:
When a class inherits from more than one parent class, it is called as multiple inheritance. It works in the same way as single inheritance.




# Python code to demonstrate multiple inheritance
  
# first parent class
class Person(object):                  
      def __init__(self, name, idnumber):
            self.name = name
            self.idnumber = idnumber
  
# second parent class      
class Employee(object):                
      def __init__(self, salary, post):
            self.salary = salary
            self.post = post
  
# inheritance from both the parent classes      
class Leader(Person, Employee):        
      def __init__(self, name, idnumber, salary, post, points):
            self.points = points
            Person.__init__(self, name, idnumber)
            Employee.__init__(self, salary, post)   


Output:


While creating an instance of a class, make sure that the sequence in which you give the parameters to a function is relevant to that within the block of the class. For example, in the above code, if we have to generate an instance, we would write

ins = Leader('Rahul', 882016, 75000, 'Assistant Manager', 560)

If you interchange the sequence position of 75000 and ‘Assistant Manager’, the code would take ‘Assistant Manager’ as the salary and 75000 as the post.
See for yourself.




# first parent class
class Person(object):                 
      def __init__(self, name, idnumber):
            self.name = name
            self.idnumber = idnumber
  
# second parent class      
class Employee(object):                
      def __init__(self, salary, post):
            self.salary = salary
            self.post = post
  
# inheritance from both the parent classes      
class Leader(Person, Employee):        
      def __init__(self, name, idnumber, salary, post, points):
            self.points = points
            Person.__init__(self, name, idnumber)
            Employee.__init__(self, salary, post)
            print(self.salary)
        
ins = Leader('Rahul', 882016, 'Assistant Manager', 75000, 560)


Output:

Assistant Manager

Overriding Methods:
Overriding a method means redefining a method in the subclass when it has already been defined in some other class.
A method in the subclass would be called as overridden only when there exists another method with the same name and same set of parameters in the superclass.
Also, we cannot override a private method of a superclass, which is the one having double underscores before its name.
For example:




# Base Class
class A(object):                
        def __init__(self):
                constant1 = 1
        def method1(self):
                print('method1 of class A')
  
class B(A):
        def __init__(self):
                constant2 = 2
                self.calling1()
                A. __init__(self)
        def method1(self):
                print('method1 of class B')
        def calling1(self):
                self.method1()
                A.method1(self)
b = B()


Output:

method1 of class B
method1 of class A

The code invokes the method1 of class B and not A because Python searches for the function in the bottom to top order.
If you want to invoke the method1 of class A, replace self.method1() with A.method1(self).
The above procedure for overriding methods works in old-style classes, which are the classes where the parent class doesn’t inherit from the ‘object’ class.
For new-style classes, where the parent class inherits from the built-in ‘object’ class, there is another procedure for overriding methods.
The super() method helps us in overriding methods in new style classes. Its syntax is as follows:
super(class_name, instance_)of_class).overridden_method_name()
Let us assume there are 3 classes A, B, and C. All 3 of them have a common function called ‘method1’. Here comes the work of super().




class A(object):
        def function1(self):
                print 'function of class A'
class B(A):
        def function1(self):
                print 'function of class B'
                super(B, self).function1()
class C(B):
        def function1(self):
                print 'function of class C'
                super(C, self).function1()
j = C()
j.function1()


Output:

function of class C
function of class B
function of class A

The ‘self’ parameter within super function acts as the object of the parent class and hence invokes the function1 of the parent class.

RELATED ARTICLES

Most Popular

Recent Comments