Amazon Interview Question
Software Engineer in TestsLet me explain this way:
Your dad has a bike and ur his son want to use his bike but it is outdated and u want to modify it the way you want to and ride it...that is, ur dad(class) has a function called Bike and ur using the same Bike(function) but modified/changed and ur using it(overriding). Same bike but now it is changed.... and when we say son it is ur modified bike that shud come into picture ..which has overriden ur dad's old bike
Overriding is useful when you want to change the functionality of some method based on a class. For example, by overriding the toString() method of Object class, we can use it with different classes.
Overloading is useful to add flexibility to a method. In general, if you want to create a method that adds two numbers then it only take either integer, double, or float but by overloading, you can add values of all data types.
I am not good in english so please kind.
Function Overloading vs Function Overriding
Function Overloading : Same function name more than once in a same class but with differnet argument , like int add(int ,int); float float add(float,float);
Here function name is same but both take different argument.
Function Overriding : Same Function name with same argument(parameter and return type) but in different classes.
But why its needed?
If you want to add some more functionality in function at the same type you also must be needed the previous one , so add more functionality in sub-class funnction.
it depends on language you are using, overriding in Java is polymorphism (as non-static member functions are virtual by default), overriding in C++ is not, if you don't explicitly using "virtual" keyword. In C++ overriding without virtual will not give you dynamic dispatching, e.g. call of fucntion depending on "real" run-time type of pointer/reference you are calling, but instead will call function depending on static/declared type.
So you must ask first - what do you mean - virtual function overriding or just overloading.
Overriding : Functions with the same definition have different behavior in different scopes. Used by a child class to change behavior it inherited from its parent.
- Anonymous June 22, 2010Overloading : More than one definition for a function in the same scope that work on different inputs. Used to have one function that can operate on different data types, especially for operators. For example overloading add() would allow two integers to be added or two strings to be added.