Few subclasses extend my abstract Parent class. The subclasses have few the same attributes e.g. date. So my question is should I declare the setters and getters for these attributes (date) in my abstract class or just in each subclass?
In one subclass it is “release date” in other “date” so should I “cast” it all to just date?
Few subclasses extend my abstract Parent class. The subclasses have few the same attributes
Share
Depends.
Many suggests use of composition (ie: Has a) rather than inheritance (ie: Is a). And most of the time, it depends what you want to achieve. There are pros and cons to both approach.
If you want to put those properties to base class then all classes has those properties, but you are saying not all of them has it. As a good design, if you are not fully populating an object, ie, you leave some fields null or empty, in purpose that is not good. Your object should be fully populated.
So what you can do is, you can make wrap properties into behaviors (another objects) and delegate them to the classes who want to use those.
Use your intuition, whichever feels right and simplify your life go with it.