When creating classes, is there a rule for when to use inheritance and when to import a new class, without inheritance, into another?
Here’s an example:
I make a class called Person
, and then create lots of Person
objects.
I then create a child class called House
. Using inheritance and properties, all my Person
objects can now have a House
.
I then create a child class called Car
so all my Person
objects now have House
s and Car
s.
and so on… and so on….
I now have this sequence of classes:
NSObject < Person < House < Car < new Class < another new Class, etc.
With the above scenario, my logic (I'm an Objective-C beginner) tells me I have two different ways of producing the same outcome:
- As explained above, or
- Create each class without inheritance and then import it into
Person
as an ivar – for example, an instance ofHouse
now becomes a type, and that is then imported into aPerson
object using properties.
Please excuse my lack of terminology and understanding. If required, I can upload a code example but it’s more of a general question on when and when not to use inheritance.