Advertisement
gauravssnl

inheritance.py

Oct 31st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # Inheritance.py
  2. """ script shows all types of inheritance in Python.
  3. 3 ways that parent and child classes can interact:
  4. 1.Actions on child imply an action on parent.(Implict Inheritance)
  5. 2.Actions on child override action on parent. (Override Explicitly)
  6. 3.Acthons on child alter the action on parent.(Alter Before or After)
  7. """
  8.  
  9. class Parent(object):
  10.    
  11.     def implicit(self):
  12.         print "PARENT implicit()"
  13.    
  14.     def override(self):
  15.         print "PARENT override()"
  16.    
  17.     def altered(self):
  18.         print "PARENT altered()"        
  19.        
  20. class Child(Parent):
  21.    
  22.     def override(self):
  23.         print "CHILD override()"
  24.    
  25.     def altered(self):
  26.         print "CHILD belfore PARENT altered()"
  27.         super(Child,self).altered()
  28.         print "CHILD after PARENT altered()"
  29.            
  30.  
  31.  
  32. dad = Parent()
  33. son = Child()
  34. # Implict Inheritance
  35. print "Implicit Inheritance Shown"
  36. dad.implicit()
  37. son.implicit()            
  38.  
  39. # Override Explicitly
  40. print  "Override Explicitly Shown"
  41. dad.override()
  42. son.override()
  43.  
  44. print "Altered Before or After Shown"
  45. dad.altered()
  46. son.altered()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement