Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip a super class when calling a super method?

This may seem strange, but I would like to do the following:

Class A

- (void)someMethod;

Class B : A

- (void)someMethod; // overrides

Class C : B

- (void)someMethod; // overrides

In class C, I would like to override a method present in the two super classes, but call only Class A's method on a [super methodName] call.

- (void)someMethod
{
  [super someMethod];  // but I want to call class A, not B
}

Possible?

like image 427
TigerCoding Avatar asked Dec 03 '11 11:12

TigerCoding


People also ask

Can you call a method in a super super class?

Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class.

What does calling the super () method do?

Definition and Usage The super keyword refers to superclass (parent) objects. 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.

Is super () called automatically?

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() .

Can we call super super method in Java?

The super keyword in Java is a reference variable that is used to refer parent class objects. The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.


1 Answers

Something like this should work:

// in Class C:
#import <objc/runtime.h>

- (void)someMethod
{
    // I want to call class A's implementation of this method

    IMP method = class_getMethodImplementation([ClassA class], _cmd);

    method(self, _cmd);

}
like image 177
Firoze Lafeer Avatar answered Oct 14 '22 04:10

Firoze Lafeer