Example of class inheritance and method overriding in Python


/ Published in: Python
Save to your folder(s)

This simple example will show you how to inherit a class from a parent class. I have to apologise for some grammar mistakes that I've probably put in the comments, but English is not my native language.

If you execute this code, the output will be:


Here comes Lois Lane

Here comes Jimmy Olsen

Here comes Clark Kent

...but his secret identity is 'Superman' and he's a super-hero!


--> Let's see what a man can do:


Jimmy Olsen walks

Lois Lane says: 'Oh no, we're in danger!'

--> Let's see what a superman can do:


Clark Kent walks

Clark Kent says: 'This is a job for SUPERMAN!'

Superman run at the speed of light

Superman fly up in the sky

Superman uses his x-ray vision


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2.  
  3. class man(object):
  4.  
  5. # name of the man
  6. name = ""
  7.  
  8. def __init__(self, P_name):
  9. """ Class constructor """
  10. self.name = P_name
  11. print("Here comes " + self.name)
  12.  
  13. def talk(self, P_message):
  14. print(self.name + " says: '" + P_message + "'")
  15.  
  16. def walk(self):
  17. """ This let an instance of a man to walk """
  18. print(self.name + " walks")
  19.  
  20. # This class inherits from Man class
  21. # A superman has all the powers of a man (A.K.A. Methods and Properties in our case ;-)
  22. class superman(man):
  23.  
  24. # Name of his secret identity
  25. secret_identity = ""
  26.  
  27. def __init__(self, P_name, P_secret_identity):
  28. """ Class constructor that overrides its parent class constructor"""
  29. # Invokes the class constructor of the parent class #
  30. super(superman, self).__init__(P_name)
  31. # Now let's add a secret identity
  32. self.secret_identity = P_secret_identity
  33. print("...but his secret identity is '" + self.secret_identity + "' and he's a super-hero!")
  34.  
  35. def walk(self, P_super_speed = False):
  36. # Overrides the normal walk, because a superman can walk at a normal
  37. # pace or run at the speed of light!
  38. if (not P_super_speed): super(superman, self).walk()
  39. else: print(self.secret_identity + " run at the speed of light")
  40.  
  41. def fly(self):
  42. """ This let an instance of a superman to fly """
  43. # No man can do this!
  44. print(self.secret_identity + " fly up in the sky")
  45.  
  46. def x_ray(self):
  47. """ This let an instance of a superman to use his x-ray vision """
  48. # No man can do this!
  49. print(self.secret_identity + " uses his x-ray vision")
  50.  
  51.  
  52. # Declare some instances of man and superman
  53. lois = man("Lois Lane")
  54. jimmy = man("Jimmy Olsen")
  55. clark = superman("Clark Kent", "Superman")
  56.  
  57. # Let's puth them into action!
  58.  
  59. print("\n--> Let's see what a man can do:\n")
  60. jimmy.walk()
  61. lois.talk("Oh no, we're in danger!")
  62.  
  63. print("\n--> Let's see what a superman can do:\n")
  64. clark.walk()
  65. clark.talk("This is a job for SUPERMAN!")
  66. clark.walk(True)
  67. clark.fly()
  68. clark.x_ray()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.