An example of an incorrect use of inheritance and composition in Java _java

Source: Internet
Author: User
Tags abstract inheritance

Believe that most people are familiar with the Java "inheritance" and "combination" of these two things, this article is mainly on these two topics to talk about. If I do not write in some places, or more naïve, the argument is not clear, you are welcome to comment.

What is "combination" and "inheritance"

Suppose there are 2 class: A and B :

    • If Class A extends B then we say that a inherits B,a is a subclass, B is a parent class, and that is inheritance.
    • If the type of a property in A is B, then we say this is a combination.

Under what circumstances are they used, respectively?

Think back to some of the things we normally consider about these two items? I think about it a little bit, and often there are the following scenarios:

    • Common code
    • To show the "commonality" between things, or simply to "generics", extract abstract class orinterface

I think there are really only two situations, but there's a special connection between the two situations, such as the Common code thing, abstract class and interface (default method in Java 8) can be achieved.

Well, having said so much, I'll just throw my opinion, and welcome to the brick:

    • If you just want public code, and the class or method that uses the common code doesn't have a clear connection, use a combination.
    • If a number of classes or their method have a more obvious connection, please extract one abstract class orinterface
    • with caution to attribute inheritance, it is recommended that subclasses explicitly copy the properties of the parent class required for the replication. This is particularly important and there is an example to illustrate the downside of this situation.

Antithesis

A more common example: We tend to define good or say in actual projects, POJO model and these model tend to have some attributes of the same noun and type, such as:

// db table primary key
private int id;

Very common, but I have encountered many colleagues in the actual work, the system defines a name may be BaseModel or RootModel the class, put the above attributes id in the inside, and then the entire project all the model inherits the Basemoddel class. I wonder if you have ever met a colleague like that? What do you think is good and bad about this writing?

First of all, if you want to say what the benefits, in addition to less knock a few keyboard, in the subclass less of these attributes, do not see any substantial benefits. But it has brought a lot of trouble to the maintenance of the project's follow-up.

And then say the potential problem of this type of wording:

One day, for some reason, you want to find out where the attributes in the subclass a (inherited from Basemodel) id are used in the project

Wit you skillfully use the IDE find usages , and then you will find that you find the use of a lot of places, and a lot of it is not your concern. But no way, you also searched for the location of the property IDs of the other classes that inherit the Basemodel. If the project is not large, may be less proficient in the search, if the project is a bit bigger? What do you do next when your search is more proficient than 50?

    • A look at the search out of the code, looked at more than 10 20 start to spray, and then continue to look down one by one?
    • Start spraying directly, and then look at a search out of the code?

How to avoid this, that is, don't use BaseModel attribute inheritance in such a way. Of course, in order to rigorous period, I still need to elaborate on this meaning, I did not completely opposed to attribute inheritance Oh, clear:

I'm against the whole project. All model inherits a Basemodel, then puts the common attribute in the Basemodel

The idea that attention is the whole project

Postscript
Examples of the above example I personally often encounter, so to come up with a single point, I am not sure if the project has been the case. Anyway, I've been so much more than my colleagues have done.

As for the "combination" and "inheritance" Other related common errors, I have not thought well (at least I think no one will commit), if I think after the follow-up, or the reader friends have other suggestions want to be able to exchange messages AH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.