fork download
  1. # your code goes here
  2. #!/usr/bin/python
  3.  
  4. class Parent: # define parent class
  5. parentAttr = 100
  6. def __init__(self):
  7. print "Calling parent constructor"
  8.  
  9. def parentMethod(self):
  10. print 'Calling parent method'
  11.  
  12. def setAttr(self, attr):
  13. Parent.parentAttr = attr
  14.  
  15. def getAttr(self):
  16. print "Parent attribute :", Parent.parentAttr
  17.  
  18. class Child(Parent): # define child class
  19. def __init__(self):
  20. print "Calling child constructor"
  21.  
  22. def childMethod(self):
  23. print 'Calling child method'
  24.  
  25. c = Child() # instance of child
  26. c.childMethod() # child calls its method
  27. c.parentMethod() # calls parent's method
  28. c.setAttr(200) # again call parent's method
  29. c.getAttr() # again call parent's method
Success #stdin #stdout 0.02s 7096KB
stdin
Standard input is empty
stdout
Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200