15

How to declare the virtual functions in Objective C.

virtual void A(int s);

How to declare the same in Objective C.

-(void)A:(int)s //normal declaration
Vladimir
  • 170,431
  • 36
  • 387
  • 313
spandana
  • 755
  • 1
  • 13
  • 29
  • 7
    All objective-C methods are virtual. Functions are construct from C language, those cannot be virtual. – Peter Štibraný May 10 '11 at 14:41
  • 2
    possible duplicate of [What is the equivalent of a C++ pure-virtual function in Objective-C?](http://stackoverflow.com/questions/4374677/what-is-the-equivalent-of-a-c-pure-virtual-function-in-objective-c) – Brad Larson May 10 '11 at 15:35

1 Answers1

43

Objective-c does not support virtual functions, or to say that another way - all functions in obj-c classes are virtual as method calls are determined in run-time.

If your subclass overrides method from superclass and you reference subclass instance using pointer to superclass then subclass method will get called:

@interface A{
}
-(void) someMethod;
@end

@interface B : A{
}
-(void) someMethod;
@end

...
A* obj = [[B alloc] init];
[obj someMethod]; // method implementation from B will be called
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Vladimir
  • 170,431
  • 36
  • 387
  • 313