views:

678

answers:

4

I'm extending the functionality of a class with a subclass, and I'm doing some dirty stuff that make superclass methods dangerous (app will hang in a loop) in the context of the subclass. I know it's not a genius idea, but I'm going for the low-hanging fruit, right now it's gonna save me some time. Oh it's a dirty job, but someone's gotta do it.

Bottom line, I need to either block that method from outside, or throw an exception when it's called directly to the superclass. (But I still use it from the subclass, except with care).

What would be the best way to do this?

UPDATE ---

So this is what I went for. I'm not self-answering, as Boaz' answer mentions multiple valid ways to do this, this is just the way that suited me. In the subclass, I overrode the method like this:

- (int)dangerousMethod
{
  [NSException raise:@"Danger!" format:@"Do not call send this method directly to this subclass"];
  return nil;
}

I'm marking this as answered, but evidently that doesn't mean it's closed, further suggestions are welcome.

A: 

If you create the methods in your superclass as "private" then the subclass has no possible way of calling them. I'm not familiar with Objective C, but every other object oriented language I've seen has the "private" qualifier.

Kibbee
Forgot to mention, I need that method to be public on the superclass. I'll edit.
Steph Thirion
Why does it need to be public in the superclass? If it's a callback method, then create an adapter class to receive the callback and hide the adapter as a private object.
Mr Fooz
Sorry, there's actually no public/private methods in objc. The adapter concept is interesting, I had never heard of it. But personally I don't want to complicate this more, I'd rather go with throwing an exception.
Steph Thirion
I love getting modded down for an answer that used to be right, until the poster went and changed the question.
Kibbee
Kibbee, I'm sorry I put the question poorly, and I tried my best to fix it that by editing, but I think it's uncalled for here, as it has nothing to do with your negative score. This has always been about objc, and objc does not have private methods.
Steph Thirion
+2  A: 

Just re-implement the unsafe method in your subclass and have it do nothing or throw an exception or re-implement it as safe, just as long as the new implementation doesn't call the unsafe superclass method.

For the C++ crew in here: Objective C doesn't let you mark methods as private. You can use its category system to split up the interface into separate files (thus hiding 'private' ones), but all methods on a class are public.

Boaz Stuller
I'm not sure if that would work. If you did what you stated, and then called some "safe" function, located in the super class, from the sub-class, which in turn called an "unsafe" function, then the do-nothing/exception method in the subclass would be called due to polymorphism.
Kibbee
Sounds right to me. As you said, as long as I never call the unsafe superclass method from the superclass. It's an imperfect solution for a far from perfect practice, but it might be the best I can get.
Steph Thirion
A: 

hi Steph,

note: I'm ObjC/Cocoa newcomer:

@implementation MyClass
-(void)myMethod:(NSString *)txt{
    if([self class] != [MyClass class]) return;
    NSLog(@"Hello");
}
@end

Peter

hey peter, this would do the job, except.. what if I have another subclass that does need that method?
Steph Thirion
A: 

This article explains how to create private variables in Objective C. They aren't truly private, but from what I read, the compiler will throw a warning if you try to call them from the subclass.

Kibbee
Hey Kibbee, I know that procedure. As you said, it doesn't make them private. It only throws warnings. Means you can still compile code with calls to these methods. And just for correctness, in objc variables can be private, methods can't.
Steph Thirion