Okay, so let's say I have a class file called Orange, and then two separate class files called Color, and Fruit.
Inside Orange, there are some properties for color, size, ripeness etc. and the method setSize(int size).
Inside Fruit, there are some properties for name, type of fruit, whether or not it has seeds, where it was imported from etc. and then the methods setName(String name), eat(), wash() and give(Person person).
Inside Color there are some properties for name, RGB values, Hex values, Alpha values etc. and then the methods setName(), setAlpha(int alpha), setRGB(int r, int g, int b) and setHex(int hex).
Most of the above mentioned information is irrelevant, and solely there to assist you in understanding. The Orange Class file has an object in it called "Parent". What I need to know is, is it possible for the object "Parent" to be set to either a Fruit, or a Color? Later on in the main class file there comes a point where myOrange.Parent.setName("Bob") happens, and in both cases (whether Parent is a Fruit or a Color) it would be possible, because Fruit has a method called setName, and Color does as well.
Thanks
Type Inheritance ("class overloading"?) represents an "is-a" relationship.
The Orange is a Fruit. No additional variables are needed; inherited members are accessed via this.
Composition represents a "has-a" relationship.
The Orange has a Color. A separate variable is declared when using this pattern, so this is clearly what is being used for Parent.
Thus Parent (a horribly named variable) is an instance of Color.
It really depends on your use case. If the "Orange" class is representing the color orange, then it might make sense for it to inherit from the "Color" class. If it represents the fruit, then it might make sense to inherit from the "Fruit" class.
However, in neither case (at least with the information given) do I see a reason for the "Orange" class to have a "Parent" reference. It would make more sense (again, depending on your use case) for your "Orange" class to extend either Color or Fruit, as I said before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With