Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Method Overriding

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Method Overriding

      You can always override your parent class methods. One reason for overriding parent's methods is that you may want special or different functionality in your subclass.

      Example

      class Parent: # define parent class
      def myMethod(self):
      print ('Calling parent method')
      
      class Child(Parent): # define child class
      def myMethod(self):
      print ('Calling child method')
      
      c = Child() # instance of child
      c.myMethod() # child calls overridden method
      When the above code is executed, it produces the following output
      Calling child method
      To understand inheritance in Python, let us take another example. We use following Employee class as parent class
      class Employee:
      def __init__(self,nm, sal):
      self.name=nm
      self.salary=sal
      def getName(self):
      return self.name
      def getSalary(self):
      return self.salary
      Next, we define a SalesOfficer class that uses Employee as parent class. It inherits the instance variables name and salary from the parent. Additionally, the child class has one more instance variable incentive.
      We shall use built-in function super() that returns reference of the parent class and call the parent constructor within the child constructor __init__() method.
      class SalesOfficer(Employee):
      def __init__(self,nm, sal, inc):
      super().__init__(nm,sal)
      self.incnt=inc
      def getSalary(self):
      return self.salary+self.incnt
      The getSalary() method is overridden to add the incentive to salary.

      Example

      Declare the object of parent and child classes and see the effect of overriding. Complete code is below
      class Employee:
      def __init__(self,nm, sal):
      self.name=nm
      self.salary=sal
      def getName(self):
      return self.name
      def getSalary(self):
      return self.salary
      
      class SalesOfficer(Employee):
      def __init__(self,nm, sal, inc):
      super().__init__(nm,sal)
      self.incnt=inc
      def getSalary(self):
      return self.salary+self.incnt
      
      e1=Employee("Rajesh", 9000)
      print ("Total salary for {} is Rs {}".format(e1.getName(),e1.getSalary()))
      s1=SalesOfficer('Kiran', 10000, 1000)
      print ("Total salary for {} is Rs {}".format(s1.getName(),s1.getSalary()))
      When you execute this code, it will produce the following output
      Total salary for Rajesh is Rs 9000
      Total salary for Kiran is Rs 11000

      Base Overridable Methods

      The following table lists some generic functionality of the object class, which is the parent class for all Python classes. You can override these methods in your own class
      Sr.No
      Method, Description & Sample Call
      1
      AI
      Constructor (with any optional arguments)
      Sample Call : obj = className(args)
      2
      AI
      Destructor, deletes an object
      Sample Call : del obj
      3
      AI
      Evaluatable string representation
      Sample Call : repr(obj)
      4
      AI
      Printable string representation
      Sample Call : str(obj)