Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method of super.super?

People also ask

How do you call super class super method?

Use of super() to access superclass constructor As we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword.

How do you call a method of super of super class in Java?

a = new AExtension(); } @Overwrite public void foo() { a. foo(); } private class AExtension extends A { } } This way you will be able to not only call the super super method but also combine calls to other super super class methods with calls to methods of the super class or the class itself by using `C. super` or `C.

Can we call super in method?

2) super can be used to invoke parent class method The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

What does calling the super () method do?

Definition and Usage It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.


In your particular example, +superclass is actually the way to go:

+ (id)someClassMethod {
    return [[[self superclass] superclass] someClassMethod];
}

since it is a class method, hence self refers to the class object where +someClassMethod is being defined.

On the other hand, things get a tad more complicated in instance methods. One solution is to get a pointer to the method implementation in the supersuper (grandparent) class. For instance:

- (id)someInstanceMethod {
    Class granny = [[self superclass] superclass];
    IMP grannyImp = class_getMethodImplementation(granny, _cmd);
    return grannyImp(self, _cmd);
}

Similarly to the class method example, +superclass is sent twice to obtain the supersuperclass. IMP is a pointer to a method, and we obtain an IMP to the method whose name is the same as the current one (-someInstaceMethod) but pointing to the implementation in the supersuperclass, and then call it. Note that you’d need to tweak this in case there are method arguments and return values different from id.


Thanks to Bavarious who inspired me to involve some runtime staff.

Briefly, the desired hypothetical line:

return [super.super alloc];

can be transformed in this "real" one:

return method_getImplementation(class_getClassMethod([[self superclass] superclass], _cmd))([self class], _cmd);

To make it relatively more clear, it can be expanded as follow:

Method grannyMethod = class_getClassMethod([[self superclass] superclass], _cmd);
IMP grannyImp = method_getImplementation(grannyMethod);
return grannyImp([self class], _cmd);