aneuryzm
aneuryzm

Reputation: 64844

About methods overriding a method in the superclass without implementation

I have a class with several subclasses.

They all override a class method, but I don't have a specific implementation for the method in the superclass.

Since I can't just declare it in the interface but I need to implement it as well (to avoid debugger warnings), I was wondering if I can just provide empty implementations of the method in the superclass.

The reason why I'm adding the methods definitions to the superclass is that I've a multi-target project, the current application delegate is considered with the specific overridden method:

[(GenericDelegate *)[NSApp delegate] myMethod];

thanks

Upvotes: 1

Views: 1544

Answers (3)

Matthew Gillingham
Matthew Gillingham

Reputation: 3429

Yes, this is a perfect normal practice. In fact, it has a name: a "Template Method." You search for that in the Cocoa documentation.

You will find that Apple also does it occasionally in their own code. The drawRect: method in UIView is the first one that comes to mind.

So, anyway, yes, if it suits your needs, I would go ahead and do it. Just make sure that you think through whether or not, for example, a protocol wouldn't suit your needs better.

Upvotes: 2

ipmcc
ipmcc

Reputation: 29926

There are other options as well. Check out the answer/discussion over here: Does Objective-C have something like C++ virtual functions?

Upvotes: 1

jbat100
jbat100

Reputation: 16827

All methods are virtual in objective c, "pure" virtual (as in C++) function don't exist and hence the equivalent methods in objective c need an empty implementation in the superclass, just to silence the compiler warning (I don't think there is any other way to do so). There is nothing wrong with that. This post is related to your question.

Upvotes: 0

Related Questions