Reputation: 3573
this is a general question. I can divide my questions into two :
I have a class ChildClass that herits from ParentClass.
1) I would like to define a initFromParentClass
method, whose signature would be
- (id) initFromParentClass:(ParentClass)anObjectFromTheParentClass
which would create a new instance of ChildClass
, based upon anObjectFromTheParentClass
— that is, all the attributes of the new instance are "copied" from the ones of the "parent instanc".
2) Can these two parent/child objects be dynamically linked ? Ie : if the parent changes, the child changes ?
Thank you
Upvotes: 0
Views: 135
Reputation: 2080
One familiar idiom is to treat let the child class delegate existing methods to an instance of the parent:
@implementation ChildClass
@property (nonatomic, strong) ParentClass *obj;
- (void) mill
{
[obj mill];
}
- (NSInteger) count
{
return [obj count];
}
This can be tedious and hard to maintain if ParentClass has a broad and rapidly-changing API. In that case, you might not want to inherit at all, but rather to extract a common interface from ParentClass and ChildClass, and perhaps let instances of ChildClass observe changes to the object from which they were initialized.
Upvotes: 1