(I think this is a pretty basic question on OOP, but unfortunately I wasn’t able to find it — so, please close this question if it’s a dublicate.)
I want to create a class, main purpose of which is to be a container of array of objects of another class. Users should be able to access this objects pretty easy, as a regular array — however, there should be custom logic on top of array logic.
Should I create a custom class from scratch and use the standard array class as a member, or should I sub-class the standard array class and include my custom logic into the overload of existing methods as well as new ones?
(I have Objective C in mind, but I don’t think that it has a lot to have with language, so I’m not including in tags).
If your class respects the whole array interface, whichever it is in your context, yes you can subclass it. If your “custom logic on top of array logic” changes the contract of array’s interface, you shouldn’t subclass it.
If you subclass Array, your class can be seen as an array. Some methods that don’t know your class can treat an instance of it as an Array. That plays a part in polymorphism. Although your class can have “custom logic”, Array’s interface cannot be broken.
Note: answered in a generic context, not specific to any language nor even specific to Array class. This is valid to any class that allows extensions.