Method overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.
Here is the explanation Assume we have two class
FirstClass -> Superclass
SecondClass -> Subclass
puts "Inside first class" end end class SecondClass < FirstClass def processtext puts "Inside second class" end end a = SecondClass.new a.processtext output : Inside second class
Firstclass has been inherited to SecondClass. So alternatively we can use all FirstClass methods using SecondClass. In SecondClass we override FirstClass method processtext with same method name. When we call processtext method with SecondClass instance we got ‘Inside second class’. It will not trigger FirstClass method processtext.
class FirstClass def processtext puts "Inside first class" end end class SecondClass < FirstClass def processtext super puts "Inside second class" end end a = SecondClass.new a.processtext output : Inside first class Inside second class
Same like above example additionally we add ‘super’ command to Second Class method processtext. We given instruction to that method ‘look on superclass and if same method found means yield those actions into this method. So it will run both actions.