nullable, nonnull, can a subclass change its superclass' rules?

When you declare designated initializers in ObjC the compiler warns if you don't override the superclasses designated initializer even if you don't need it.


I have a class named say, InputInformation. Now InputInformation has several properties related to input (of course). Now I have another class,which is a subclass of InputInformation say OutputResult. Now lets say I have a class call Brain:

@interface Brain : NSObject

//all kinds of stuff..

-(OutResult*)generateOutputFromInput:(InputInformation*)input;

@end


I choose to make output result a subclass of InputInfo because it uses many, but not all of Input's properties.


Now the InputInformation class has a desginated initializer that takes a bunch of parameters. Output has a designated initializer. and in the OutputResult class InputInformation's designated initializer is marked not allowed, to prevent compiler warnings. So Output overrides input's designated initializer to force a crash just in case someone uses it incorrectly.


Now OutputResult's designated initializer must call its super classes designated initializer. There are parameters that shouldn't be nil if the object is an InputInformation object, but should be nil if the object is an OutputResult object. Calling super and passing nil into nonnull params generates warnings...is there a way for a subclass to override and change nonnull, nullable rules of its superclass? I want to get the warnings for Input objects, but I don't want them on OutResult objects.

Answered by Frameworks Engineer in 73431022

No, you can't change the nullability rules of a superclass. There are problems going both ways by doing that.


If the super class declares a property nullable, and the subclass declares it nonnull, then a client assuming super class instances might try to set the property to nil, but the instance only expects non-nil values, and thus break the subclass' contract.


If the super class declares a property nonnull, and the subclass declare it nullable, then the subclass setting that value to nil might break logic in the super class that relies on the value being nonnull.

Accepted Answer

No, you can't change the nullability rules of a superclass. There are problems going both ways by doing that.


If the super class declares a property nullable, and the subclass declares it nonnull, then a client assuming super class instances might try to set the property to nil, but the instance only expects non-nil values, and thus break the subclass' contract.


If the super class declares a property nonnull, and the subclass declare it nullable, then the subclass setting that value to nil might break logic in the super class that relies on the value being nonnull.

I see.


I'll have to stop fighting it and make Output not be a subclass of Input, and just either redeclare the shared properties in Output that have the same name and attributes or move them to protocol or a base class. I'm still used to the more layed back ObjC I guess...where this stuff is not strictly enforced. Since this is all my code I sort of know changing the attributes wouldn't break. Input and Output conforms to NSSecureCoding...also kind of liked having Output inherit so it can inherit some decoding/encoding from the superclass (probably go with a base class).


I do think these attributes are useful for catching bugs so I do want to use them.

nullable, nonnull, can a subclass change its superclass' rules?
 
 
Q