I have method overriding like below. Please ignore the way i written it may not be perfectly written, just for sample. But i want to know what would the output if i method overridden like this for variable “a”.
-> base class
@interface A
{
int a=15;
}
-(int) myFunction;
@end
-> derived class
@interface B : A
{
int a=10;
}
-(int) myFunction;
@end
Lets image “myFunction” returns “a”. Since, it does method overriding, when i call like,
B bObj;
bObj.a = ?
What should be the output 10 or 15 ?
I have assumed you are aware you can’t declare variables in a header like that and the initialisation there is just for simplicity.
Similarly, your
B bObjhasn’t been initialised and isn’t a pointerbObj.awould return 10. So would[bObj myFunction]. You have overridden the method and told the compiler to disregard previous implementations of this method and use the new one.Can you suggest what the output of this might be:
or this: