-1

So lets say i have a class Fruit and a Subclass called Apple. In the Fruit class is some kind of variable lets say a List of fruits or whatever. And i need that list of fruits in the apple class. So i know if i write extends Fruit on the Apple class then i get access to the fruit list from the super class (assuming the list is protected or public etc). So should i use inheritance to get the fruit list or should i use dependency injection to get the list somehow into the apple class? This question may be dumb but im a bit confused on this. I know that you can only extend from one class so if i needed two fruit lists or something else then i couldn't get the other one with inheritance. What i am doing right now in most classes is just from the main class im just getting the fruit list from the fruit class and then injecting it from there into all other classes that need it. Not sure if i inject the reference to the object or just the values there aswell.

So like when do i use dependency injection and when inheritance?

Stitchie
  • 1
  • 3
  • 4
    Genuine question to which you should have a good answer, and which should be in your post: _why_ would your Apple class need access to the entire list of fruit? Anything it might be tempted to do with that list is something that the Fruit class should be doing instead. – Mike 'Pomax' Kamermans Apr 27 '21 at 22:29
  • well its just an example, im coding minecraft plugin and i have a class that just registers a bunch of classes and also puts it in an EnumMap and i need to have that stuff in the map in other classes aswell because each object in it holds needed information about it but i dont really need all the other stuff from that register class, just the map but extending the class seems a lot more simple than injecting it through tons of classes – Stitchie Apr 27 '21 at 22:40
  • I mean in short i just need the map because it holds information or aka all the instances of the classes – Stitchie Apr 27 '21 at 22:42
  • 1
    As suggested by Mike, either it be part of fruit clas. Or say u need a mix-fruite list as well as an aple object together to perform certain operation, then it shud b done in separate class say "FruitMixer" which should not inherit Fruits but has a way to accept mixfruitList &apple references in method. something like use compactor instead of comparable – Aarati Apr 27 '21 at 22:46
  • Lots more discussion and some links to relevant articles here: https://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition – dnault Apr 27 '21 at 23:06
  • Please do not use "just an example" when you have questions about fundamental design patterns: please update your post with a concrete example that is so close to your actual code that comments we make about it applies to your code. Or just the actual use-case you're dealing with right now. – Mike 'Pomax' Kamermans Apr 27 '21 at 23:46

1 Answers1

1

It sounds like you're trying to do too many things in a class. Often if your design is confusing, splitting classes up can clarify it.

In this case, a fruit class defines how to be a fruit, just as an apple classs, if it inherits from fruit, defines how a fruit should be an apple. A list of different kinds of fruits is generally not something a fruit would "know" about, it just knows it's a fruit. So you'd want some sort of "fruit directory" object which is separate (and might select a subset based on whether items are selectClass.instanceof(), where selectClass would be a parameter like Apple.class).

If you think of an apple as a fruit with specific behaviour, you could add that as an interface (e.g. class Apple extends Fruit implements FruitIdentity). But a list of different fruits would still be separate from "how to be a fruit" that the Fruit class would describe.

Does that make sense?

John Bayko
  • 746
  • 4
  • 7
  • I understand what you mean and i agree but i think the example i gave is just a bad example. I really just want to get the variables or things that some random other class has to use it in another class. I have a class where i just create a bunch of instances of specific classes that all implement a certain interface. And i just need access to that list of instances in the other class. Extending that class just seems like the most straight forward way to do that. I hope you understand. – Stitchie Apr 28 '21 at 05:35
  • If you want me to go specific i have one class called PerkManager where i just create an instance of every class that implements Perk and it puts all of them in an enummap with enum Perks as key and Perk instances as values. Now i need to get this enummap into all other classes where i need this map that has these instances of classes that have functionality of the perk and information about it in it. Thats about as specific as i can get. – Stitchie Apr 28 '21 at 05:38
  • Btw in theory i could just create a new instance of these classes everytime i would use them. I don't really know if thats something you should do or not because i would just create a bunch of identical instances of that class basically that would get garbage collected instantly after it got used probably. Im guessing thats not good. – Stitchie Apr 28 '21 at 05:42
  • Now it sounds like you're trying to use a class as an instance. Java lets you store things ni classes, but that's not what they're for, that just makes them global variables, with all the inflexibility that causes. If you want a global variable tracking all your objects, it can be a singleton (that the class `getInstance()` method can return) that you can at least pass around if needed, but probably better to make a single object that is shared (dependency injected if you prefer that term). Design it so it's immutable (or has an immutable view) for more safety. – John Bayko Apr 28 '21 at 23:00