Skip to main content

Inheritance and Types in Python

 

Inheritance

 

Creating a new class from existing class is known as inheritance. The class from which features are inherited is known as base class and the class into which features are derived into is called derived class.




Syntax:

  1. class derived-class(base class):  
  2.     <class-suite>   
 


Inheritance promotes reusability of code by reusing already existing classes. 


Inheritance is used to implement is-a relationship between classes.

 

Following hierarchy is an example representing inheritance between classes:

 

vehicle-hierarchy


Single inheritance

 
When a derived class inherits only from syntax, the base class is called single inheritance. If it has one base class and one derived class it is called single inheritance.
 
Diagram
 
Types Of Inheritance In Python
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class b(A): #child class  
  5.     #some code  
Example
  1. class father:#parent class  
  2.   def father_name(self):  
  3.      print("my father name is selvaraj")  
  4.   
  5. class son(father):#child class  
  6.   pass  
  7.   
  8. obj_father= father()#object for father class.  
  9. obj_son=son()#object for son class.  
  10.   
  11. obj_son.father_name()# you access son class to father class
Output
 
Types Of Inheritance In Python
 

Multilevel inheritance

 
Multiple inheritance is the concept where a subclass inherits properties from multiple base classes. It has one base class and two (or) more derived classes called multilevel inheritance.
 
Diagram
 
Types Of Inheritance In Python
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class B(A): #child class  
  5.     #some code  
  6.   
  7. class C(B): #child class  
  8.     #some code  
Example
  1. class g_father:#parent class  
  2.   def g_father_name(self):  
  3.      print("my father name is ramasamy")  
  4.   
  5. class father(g_father):#child class  
  6.   def father_name(self):  
  7.      print("my father name is selvaraj")  
  8.   
  9. class son(father):#child class  
  10.   def son_name(self):  
  11.      print("my name is surya")  
  12.   
  13.   
  14. obj_g_father= father()#object for g_father class.  
  15. obj_father= father()#object for father class.  
  16. obj_son=son()#object for son class.  
  17.   
  18. obj_son.g_father_name()# you access son class to g_father class.  
  19. obj_son.father_name()# you access son class to father class.  
  20. obj_son.son_name()# you access same class
Output
 
Types Of Inheritance In Python
 

Multiple inheritance

 
Multiple inheritance is the concept where a subclass inherits properties from multiple base classes. If it has two base classes and one derived class it is called multilevel inheritance.
 
Diagram
 
Syntax
  1. class A: #parent class  
  2.     #some code  
  3.   
  4. class B(A): #parent class  
  5.     #some code  
  6.   
  7. class C (A, B): #child class  
  8.     #some code  
Example
  1. class father:#parent class  
  2.   def father_name(self):  
  3.      print("my father name is selvaraj")  
  4.   
  5. class mother:#parent class  
  6.   def mother_name(self):  
  7.      print("my father name is jayanthi")  
  8.   
  9. class son(mother,father):#child class  
  10.   pass  
  11.   
  12. obj_father= father()#object for father class.  
  13. obj_mother=mother()#object for mother class.  
  14. obj_son=son()#object for son class.  
  15.   
  16. obj_son.father_name()# you access son class to father class.  
  17. obj_son.mother_name()# you access son class to mother class.  
  18.   
  19.   
  20. #obj_mother.father_name()\\This statement did not work because mother and father are parents. 
Output
 
Types Of Inheritance In Python
 


Hierarchical Inheritance

This inheritance allows a class to host as a parent class for more than one child class or subclass. This provides a benefit of sharing the functioning of methods with multiple child classes, hence avoiding code duplication.

#parent class
class Parent:
    def fun1(self):
        print(“Hey there, you are in the parent class”)
 
#child class 1
class child1(Parent):
    def fun2(self):
        print(“Hey there, you are in the child class 1”)

#child class 2 
class child2(Parent):
    def fun3(self):
        print(“Hey there, you are in the child class 2”)
 
#child class 3
class child3(Parent):
    def fun4(self):
        print(“Hey there, you are in the child class 3”)
 
# main program
child_obj1 = child3()
child_obj2 = child2()
child_obj3 = child1()
child_obj1.fun1()
child_obj1.fun4()
child_obj2.fun1()
child_obj2.fun3()
child_obj3.fun1()
child_obj3.fun2()

In the above code, we have a single parent class and multiple child classes inheriting the same parent class. Now all the child classes can access the methods and variables of the parent class. We’ve created a “Parent” class and 3 child classes “child1”, “child2”, “child3”, which inherits the same parent class “Parent”.

Hybrid Inheritance: 

Inheritance consisting of multiple types of inheritance is called hybrid inheritance.

class School:
     def func1(self):
         print("This function is in school.")
  
class Student1(School):
     def func2(self):
         print("This function is in student 1. ")
  
class Student2(School):
     def func3(self):
         print("This function is in student 2.")
  
class Student3(Student1, School):
     def func4(self):
         print("This function is in student 3.")
  
# Driver's code
object = Student3()
object.func1()

object.func2()


Output:

This function is in school.
This function is in student 1.




Comments

Popular posts from this blog

Is-A and Has-A relationships in python

  In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House. #Is-A relationship --> By Inheritance class  A:    def   __init__ ( self ):      self .b= 10    def   mym1 ( self ):      print ( 'Parent method' ) class  B(A):    def   mym2 ( self ):      print ( 'Child method' ) d = B() d.mym1() #output: Parent method d.mym2() #output: Child method HAS-A Relationship:  Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom. Let’s understand these concepts with an example of Car class. # Has-A relationship --> By Composition class  Engine:    def   __init__ ( self ):      s

Magic Methods in Python

  What Are Dunder Methods ? In Python, special methods are a set of predefined methods you can use to enrich your classes.  They are easy to recognize because they start and end with double underscores, for example  __init__  or  __str__ . Dunder methods let you emulate the behavior of built-in types.  For example, to get the length of a string you can call  len('string') . But an empty class definition doesn’t support this behavior out of the box: These “dunders” or “special methods” in Python are also sometimes called “magic methods.” class NoLenSupport : pass >>> obj = NoLenSupport () >>> len ( obj ) TypeError : "object of type 'NoLenSupport' has no len()" To fix this, you can add a  __len__  dunder method to your class: class LenSupport : def __len__ ( self ): return 42 >>> obj = LenSupport () >>> len ( obj ) 42 Object Initialization:  __init__ "__init __ " is a reserv

Architechture of Kubernetes

  Kubernetes Architecture and Components: It follows the client-server architecture, from a high level, a Kubernetes environment consists of a  control plane (master) , a  distributed storage system  for keeping the cluster state consistent ( etcd ), and a number of  cluster nodes (Kubelets). We will now explore the individual components of a standard Kubernetes cluster to understand the process in greater detail. What is Master Node in Kubernetes Architecture? The Kubernetes Master (Master Node) receives input from a CLI (Command-Line Interface) or UI (User Interface) via an API. These are the commands you provide to Kubernetes. You define pods, replica sets, and services that you want Kubernetes to maintain. For example, which container image to use, which ports to expose, and how many pod replicas to run. You also provide the parameters of the desired state for the application(s) running in that cluster. API Server: The  API Server  is the front-end of the control plane and the only