Reputation: 1328
I have a superclass, SuperClassYeh and a subclass, SubClassYeh. I have method inheritTest
in SuperClassYeh and I override inheritTest
in SubClassYeh. I start the program running by calling method testSuperAndSelf
in SubClassYeh. This method would call another method, fromYEH
in SuperClassYeh. In fromYEH
, I would like to call inheritTest
in SuperClassYeh. How do I do that? Using [self inheritTest]
calls the inheritTest
in SubClassYeh, not SuperClassYeh.
Here's the code fragment to start the whole thing running
SubClassYeh *testing = [[SubClassYeh alloc] init];
[testing testSuperAndSelf];
Here's the code fragment for SuperClassYeh
- (void) fromYEH
{
[self inheritTest]; //Calls the inheritTest in SubClassYeh, not SuperClassYeh
}
- (void) inheritTest
{
NSLog(@"Testing Indicator. Inside SuperClassYEH inheritTest");
}
Here's the code fragment for SubClassYeh
- (void) inheritTest
{
NSLog(@"Testing Indicator. Inside SubClassYeh inheritTest");
}
- (void) testSuperAndSelf
{
[super fromYEH];
}
Upvotes: 0
Views: 1005
Reputation: 151
Just for the record...
What you want to do isn't as difficult as you think, if you look at it from a different perspective.
All you have to do is define a C function and call that.
- (void) fromYEH
{
inheritTest(self); //Calls the inheritTest() in SuperClassYeh
}
void inheritTest(SuperClassYeh *self)
{
NSLog(@"Testing Indicator. Inside SuperClassYEH inheritTest");
}
- (void) inheritTest
{
inheritTest(self);
}
(I love the names... ;)
Upvotes: 0
Reputation: 1328
I found the solution. Instead of calling [super fromYeh]
in testSuperAndSelf
, change the receiver to the superclass. Use [[[[SubClassYeh superclass] alloc] init] fromYEH]
. Inside method fromYEH
, the inheritTest
of SuperClassYEH
would be called.
Here is the code fragment for method testSuperAndSelf
in SubClassYeh
. Other codes remain the same.
- (void) testSuperAndSelf
{
[[[[SubClassYeh superclass] alloc] init] fromYEH];
}
Upvotes: 0
Reputation: 10860
Even it is a quite strange behavior, you can try something like this(didn't test it, actually)
- (void) fromYEH
{
bool isSubClass = [self isKindOfClass:[SubClassYeh class]];
if(isSubClass == YES)
{
[super performSelector:@selector(inheritTest)];
}
else
{
[self inheritTest];
}
}
Anyway, even if this code will really do the trick, I still think that this is very bad practice.
EDIT:
Checked this in XCode. It will not do the trick =) So upvoted Martin's answer =)
Upvotes: 0
Reputation: 539685
You can't, and this is the intended behaviour of inheritance in Objective-C.
If self
is an instance of SubClassYeh
, then
[self inheritTest]
calls the SubClassYeh
implementation of inheritTest
(if there is one),
no matter from where that message is sent (subclass or superclass).
The only difference between [super fromYEH]
and [self fromYEH]
is that the lookup
for the "fromYEH" message starts at the superclass, but it does not change the fact
that self
is an instance of SubClassYeh
.
And you cannot prevent a method from being overriden in Objective-C (see How to avoid superclass methods getting overridden by sub class in objective - c or other Q&A's about that topic).
The only thing that you can do in the superclass is to choose method names that are not overriden accidentally, e.g. by prefixing the method name with the class name, as suggested here: https://stackoverflow.com/a/17209309/1187415.
Upvotes: 5
Reputation: 4767
If you can call [super fromYEH]
from SubClassYeh, what is the problem in calling [super inheritTest]
the same way?
Upvotes: 0