Overloading vs. Overriding

Overloading

  • When making multiple methods with using same method’s name
  • Parameter has to be different

overloadingCodeoverloadingResult

Overriding

  • When trying to edit parent class’ method.
  • Method name and parameter has to be same
  • @override is not necessary, but better to write.
  • It is better to write @override before the method.
    • pros
      • Compiler will check error when you make mistake while overriding such as misspelling method name.
      • Otherwise you won’t know whether you made a mistake, and also you will get unexpected behavior
      • Readabilty both for other developers and for you

overridingCodeoverridingResult

Conclusion

Overloading: In same class, make same named methods with different return type or parameter.

Overriding: Redefining parent’s method. One of Polymorphism which is characteristic of object oriented programming .

Leave A Comment

Your email address will not be published. Required fields are marked *