In objective-c, how could it be determined in a super class if a specific method was overwritten by its (calling) child(ren)?
For example, in the below, child class Z doesn’t override method b, therefor the base class X would do some additional default processing. ZY does however implement b in child-class Y so the default processing would not be required.
// base class X
-(void)a
{
// do something
}
-(void)b
{
if("no_child_has_implemented_b") {
// add some default behavior
}
}
// child class Y : X
-(void)a
{
[super a];
}
-(void)b
{
[super b];
}
// child class Z : X
-(void)a
{
[super a];
}
// child class ZY : X
-(void)a
{
[super a];
}
In a reasonable normal OO design, you shouldn’t have a base class know if a subclass is overriding it. If you find yourself doing that, it’s a code smell, and usually means there is better way of doing things.
Changing a superclasses’ method behaviour based on whether it is overridden isn’t a good idea in most cases because it means the superclass is second-guessing the behaviour and intent of the subclass. For example, what if one day someone wrote a subclass that wanted to get the default setup afforded by the superclass, for a reason you hadn’t thought of?
It’s quite regular to have overridden methods in a subclasses call the super version, and the super version does basic setup relevant to its class, and then your subclass’ overridden method gets to do additional things after calling super. If this doesn’t handle your situation, you could just have your subclass’ method not calling the super version of the method — it would just execute its own code.