Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call static methods of parent class in Objective - C.

Question: How to call superclass static method ?

I mean directly by using:

[SuperClassName method]

OR

There is any other way existed?

like image 365
veerababu Avatar asked Mar 18 '14 11:03

veerababu


People also ask

How do you call a static method of parent class?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

Can we have same static method in child class?

We can declare static methods with the same signature in the subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'.

What is static method in Objective C?

From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. This describes exactly what Objective-C's class methods are not.

Can a parent class be static?

Overriding is when we pass a child class object to a parent class reference variable but the static methods are associated with class they are not linked with an object so we can't override a static method but we can hide a static method in java.


1 Answers

If you wants to call drive class methods from base class then, declare class method in your drive class like this: using (+) sign before method name.

+(void)myClassMethod;

Call this method from base class like this:

[YourDriveClassName myClassMethod];

Or you wants to call drive class instance methods from base class, declare instance method in your drive class using (-)sign before method name.

-(void)sayHelloToSomeOne:(NSString *)greeting;

Call this method from base class.

[super sayHelloToSomeOne:@"Hello Worlds!"];
like image 57
Gajendra Rawat Avatar answered Sep 22 '22 21:09

Gajendra Rawat