简体   繁体   中英

Should I extend a class just because one variable is added?

Imagine you have Class Person , which holds a lot of information. Now I thought about adding a new variable, which should not be accessible from the Person Class, rather I would like to extend a LivingBeing Class (this example is just for this thread, so please do not question its sense). Later on, it may be more than just one added variable, but for now, LivingBeing would have one variable which Person should not have. Person would later on become LivingBeing , when the user decides so.

I would have to tinker a lot to make this inheritance work, but most of all I wonder if you would consider it either good style or being efficient. Would it be worth to change a lot of code for this simple inheritance?

The alternative would be, to add this certain variable to Person , and simply not use it until "later on" it is needed. This seems to be bad style though, Inheritance exists for a reason and I should use it, I thought.

Edit: What may be important is that only some Person -Objects become LivingBeings , they coexist. So it would be easier to differentiate between Person and LivingBeing objects, instead of always having Person .

Generally speaking, there aren't really concrete answers to this kind of question.

If you're just building your own application and it focuses on living beings, I'd personally consider it a waste to have extra classes for non-living beings... unless there is a good business case to do so. Inheritance gets messy fast when overused or used improperly.

If you're building a flexible, re-usable library to handle a broad range of unknown cases though, you should be more strict in your application design because you can make less assumptions.

In the latter case, yes; it would be bad design to start adding extra variables to your classes which are only used in certain scenarios. That would pollute your class hierarchy.

There are lots of alternatives to inheritance though that you should consider when tackling these kinds of design issues:

  1. Mixins - many languages let you mix traits into other classes as needed. This isn't strictly built into Java, but it is possible in Java 8 with some work using interfaces. That would allow you to have extra living-being specific functions if needed in the future.

  2. Composition - In composition, the living being class would hold the person class and defer functions to it. This is generally cleaner than inheritance. https://en.wikipedia.org/wiki/Composition_over_inheritance

  3. Make an attributes hierarchy that just represents extra features a class can have. Then you could store it in some map/etc inside the person class. That way as you added more attributes, the changes in the class would be limited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM