Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if subclass overrides a method

Is it possible to check whether a subclass implements a method that exists either in its immediate superclass or in some superclass of its superclass, etc?

E.g. I subclass UIView, to make my custom generic view for my app, and then I subclass this custom view. Now some of my subclasses override some method from UIView and some don't. I only want to call this method if it is actually overridden, I do not want the default UIView method being called.

Is there a way to check this, i.e. with a method similar to respondsToSelector:?

Edit: This question is different from the one asked in Objective-C detect if class overrides inherited method because I do not really know or do not want to care which superclass originally implements the method.

like image 456
Banana Avatar asked Feb 26 '15 07:02

Banana


People also ask

How do we identify if a method is an overridden method?

getMethod("myMethod"). getDeclaringClass(); If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it.

Can subclass override methods?

Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.

Can subclass override the methods of superclass?

A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.

Which methods must be override in the subclass?

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.


2 Answers

Based on this answer by Mert Buran.

You can create a simple method to check whether a given object overrides a given selector (method):

-(BOOL)checkIfObject:(id)object overridesSelector:(SEL)selector {

    Class objSuperClass = [object superclass];
    BOOL isMethodOverridden = NO;

    while (objSuperClass != Nil) {

        isMethodOverridden = [object methodForSelector: selector] !=
        [objSuperClass instanceMethodForSelector: selector];

        if (isMethodOverridden) {
            break;
        }

        objSuperClass = [objSuperClass superclass];
    }

    return isMethodOverridden;
}

This can be called as follows:

[self checkIfObject:someObject overridesSelector:@selector(someSelector)];
like image 115
ZeMoon Avatar answered Oct 12 '22 10:10

ZeMoon


Unfortunately, there is no equivalent to respondsToSelector: that will do precisely this job for you. As you will probably know, respondsToSelector: works in the way that it will return YES as long as the class itself or any of its superclasses implements this method.

But why not just put an empty implementation of the method into your custom subclass, that way you make sure that calling it doesn't have any effect and doesn't call the same method in the superclass. Did you think about this?

Update: While there is no method equivalent to respondsToSelector:, you might want to take a look at the Objective-C Runtime. It's a library that allows you to inspect characteristics of a class during runtime (a bit similar to Java reflections). Check out the reference here.

like image 44
nburk Avatar answered Oct 12 '22 11:10

nburk