I am trying to do a class extension and calling the extended method from a generic method. I’m just wondering if it’s possible to do it? Below is my code.
ClassA.h:
@interface ClassA : NSObject
-(void) method;
ClassA.m:
#import "ClassA.h"
-(void) method{
NSLog(@"do A");
}
ClassB.h:
@interface ClassB : ClassA
-(void) method;
ClassB.m:
#import "ClassB.h"
-(void) method{
NSLog(@"do B");
}
and in MainClass.m
-(void) doMethod {
ClassA *class = [[ClassB alloc] init];
[class method];
}
I tried to do something like this, but nothing was print out. Is it possible to do this? and will it print out “do B”??
Your code compiles and runs fine (minus a small typo in @interface ClassA.h that should be @interface ClassA):
Apple’s Objective-C docs contain a chapter on how inheritance works that is available online here.