Method Overriding and Overloading
In the past few weeks, I’ve done multiple technical and non-technical interviews with team at various organizations. There was a common theme in most technical interviews, that was of object oriented programming (OOP). Although, OOP is an extensive topic in itself and there are multiple questions that was asked regarding OOP, one question came-up multiple times; method overriding vs method overloading.
Method overriding is a concept tied with inheritance, when a child class inherits some method from the parent class but redefines it on its on context then it is known as method overriding. Let us see an example:
In the above example, classes ‘Square’ and ‘Triangle’ extends class ‘Polygon’. They all have method ‘describe()’, which prints out simple definition of themselves, even though the method name and arguments are same the result is different in each classes.
When we call calculateArea and pass it single argument, it prints out the number*number and it we pass double argument, it prints out the product of the two argument, just like we intended.
So, that is all for this edition of my blog, until next time happy coding.