I believe that most people are more familiar with the two things of "inheritance" and "combination" in Java. This article will mainly discuss these two topics. If I write something wrong, or it is childish and the argument is not clear, everyone is welcome to leave a message to correct me.
Suppose there are 2 classes: A and B :
extends B then we say that A inherits B, A is a subclass, and B is a parent class, and this case is inheritance.Think back to what circumstances do we usually consider these two things? I thought about it briefly and there are often the following scenarios:
abstract class or interface is extracted After thinking about it, it seems that there are really only these two situations, but these two situations have a special relationship. For example, the public code thing can actually be achieved abstract class and interface (the default method in Java 8).
Well, after saying so much nonsense, I will just throw my point of view. Welcome to:
abstract class or interface A more common example: In actual projects, we often define POJO or model , and these models often have some attributes with the same noun and type, such as:
// db table primary keyprivate int id; It's very common, but I have met many colleagues in my actual work. The system defines a class with the name BaseModel or RootModel , puts the above attribute id in it, and then all models in the entire project inherit this BaseModdel class. I wonder if you have ever met such colleagues? What are the benefits and disadvantages of writing like this?
Let’s talk about the benefits first. If you have to say what benefits it can bring, you don’t see any substantial benefits except for typing the keyboard a few times and missing these attributes in the subclass. However, it has brought a lot of trouble to the subsequent maintenance of the project.
Then let’s talk about the potential problems of this writing:
One day, for some reasons, you want to find where to use the attribute
idin the subclass A (inheriting the BaseModel) in the project
You are smart and skillfully used find usages in the IDE, and then you will find that you have found a lot of usage locations, and many of them are not what you care about at all. But there is no way, you have also searched for the use location of the property id of other classes that inherit BaseModel. If the project is not big, you may have fewer proficiencies. What if the project is a little bigger? When you have more than 50 proficiency in search, what will you do next?
How to avoid this situation? That is, do not use BaseModel like this to use attribute inheritance. Of course, for the sake of rigorous period, I still need to explain this meaning in detail. I do not completely oppose attribute inheritance. Let's clarify:
What I object is that all models of the entire project inherit a BaseModel and then put the common properties in the BaseModel
This idea is to note that it is for the entire project
I personally often encounter examples of the negative textbooks above, so I will talk about them separately. I am not sure if this has happened in your projects. Anyway, I have been cheated by my colleagues for many times.
As for other common mistakes related to "combination" and "inheritance", I haven't thought about it yet (at least I think no one will make it). If I think it clearly in the future, or if readers have other suggestions, I hope you can leave a message to communicate.