For expert programmers this topic does not need to be explained but many people who come to Minecraft modding are beginner or intermediate and need to be reminded about this important programming topic: when should you extend a class versus copying the code from a class (versus using or creating an interface)?
When most people start Minecraft modding the first thing they do is take an existing class for a Block, Item or Entity and extend it and change a bit of it. This is a great way to start modding because it is an easy to achieve something interesting without understanding all the Minecraft (and Forge and FML) code. However, some people continue to use class extension without understanding the full implication. Perhaps it would have been better to copy the code for the class. So I'd like to educate beginners on the difference.
When Extending Classes Goes Wrong
Key Point: A class that is extended from a class is also considered to be the parent class.
For example, imagine you want to make a goat entity. You might think that an EntitySheep is pretty close to what you want, so you make your EntityGoat extend EntitySheep. You change the model and texture and everything works great, right? Wrong. Because technically any code that checks for EntitySheep will test true on your entity. For example, because your goat is also a sheep the wolves will attack your goats, your goats will be shearable, and sheep will be able to mate with your goats. It is possible that you in fact want your goats to be treated like sheep, but what if you instead extend sheep to make a giraffe -- that wouldn't make sense would it?
Conversely, if you want something to be a type of something, go ahead and extend it. Maybe you want to make a "golden ram" entity that is a sheep but gives you a "golden fleece" item. In that case, it would probably make sense to extend a sheep.
Tip: Only extend a class if you want the subclass to be fully treated like the parent class (i.e. you're making an additional type of that class).
Furthermore, in good coding practice (of course there are many points of view about good coding practice) you really shouldn't fully override parent class methods -- the point of "extending" is to add to, not to modify. In a good class hierarchy the parent class would only contain code that is common to all its subclasses. However, since we didn't write the vanilla Minecraft code, we can't always use the hierarchy perfectly so in modding we do sometimes need to extend classes that we then modify. It is still good to strive to a clean inheritance scheme though!
When To Copy A Class' Code
Let's go back to the example of making a giraffe entity. In such a case, it is probably a better idea to copy the code from the EntitySheep into your class and then modify it to suit your needs. You'll notice that the code you copied still extends something: it extends EntityAnimal. That makes sense because you likely do want your giraffe to be considered an animal (in Minecraft being an animal means you can have different ages and they can breed which seems appropriate for a giraffe).
You'll also notice that the code you copied implements the IShearable interface. Since you don't want your giraffe to be shearable, you can just delete that without problem. (You would not have been able to delete that if you had extended EntitySheep because you would inherit the interface and be forced to implement it.)
Key Point: What all this really means is that you should select what you extend to be appropriate to your need, then copy from other subclasses of that parent for anything that might be useful.
ConclusionSo overall it is pretty simple once you understand it:
- If you want something to truly be an additional type of something you should extend it.
- If you want something to be "like" something else you should extend its parent class and copy its code (then modify as needed).