Which of the following allows change in behavior of member function of a class without changing its declaration
(A) Method overloading
B) Method overriding
C) Create new class
(D) none
According to me its option (B), but I am confused, can anyone clarify it
Method overloading is having two or more methods with the same name, but differing signatures, ie different return values or parameters.
Method overriding on the other hand is having a method in a derived class have the same name and signature as a method in the super class. This allows derived classes to each have their own implementation of a given method, which could very well result in different behavior from each class. This is a super useful concept.
Well, technically, it's not changing the behaviour of a member of a class. It's creating a new, derived class, with different functionality. It's just that if you use a pointer to the base class, that points to an object of the derived class, then when you call the method, it will execute the method on the derived class, not on the base class.
But you haven't actually changed the behaviour of the method on the original class.
It all depends on how pedantic the person setting the question wants to be.
It is overloading. Basically what overloading a function does is that it lets you use the same function name and type, but be able to change what each one does depending on the parameters it was called with.
Overloading is changing the declaration, sort of. The wording on the question is pretty terrible. But I definitely go with the overriding vote since that leaves the signature the same, but provides different behavior.
I am pretty sure it is not overloading and it is overriding, though it definitely requires some assumptions such as assuming that the method is inherited from a parent class.