Relationships between Java classes

Source: Internet
Author: User

Classes and classes are divided into inheritance, implementation, dependency, association, aggregation, and composite relationships.

1. inheritance relationship

   Inheritance refers to the function that a class (subclass) inherits from another class (parent class Interface) and adds its own new functions. In Java, the inheritance relationship is identified by the keyword extends. In the UML class diagram design, the inheritance is represented by a solid line with a hollow triangle arrow, pointing from the subclass to the parent class.  

II. Implementation relationship

   Implementation refers to the function of a class to implement interface interfaces (multiple). Implementation is the most common relationship between classes and interfaces. In Java, such relationships are identified by the keyword implements. In the UML class diagram design, a dotted line with a hollow triangular arrow is used to represent the implementation interface from the class.

Iii. Dependency

   In a simple understanding, dependency refers to the use of Class A to Class B, which is contingent, temporary, and very weak, however, changes in Class B will affect Class. For example, if someone wants to cross the river, he needs to borrow a ship. At this time, the relationship between people and the ship is dependent. The Code layer shows that Class B is used by Class A in a method as a parameter. In the UML class diagram design, the dependency is composed of classes.
A points to Class B with an arrow dotted line.Criteria: local variables, method parameters, and calls to static methods.

Iv. Association

Associations reflect a strong dependency between two classes at the semantic level, such as my friends and I, such a relationship is more dependent than the dependency, and does not have any dependency relationship. The relationship is not temporary and generally long-term, and the relationship between the two parties is generally equal. Association can be unidirectional and bidirectional. As shown in the Code layer, associated Class B appears in the form of class A attributes in associated Class, it may also be that associated Class A references a global variable of the associated Class B. In the UML class diagram design, the association relationship is represented by an arrow solid line pointing from association class A to associated Class B. The roles and multiple tags of the associated parties can be marked at both ends of the association.

5. Aggregation

   Aggregation is a special case of association. It reflects the relationship between the whole and the part, that is, the relationship between has-. At this time, the whole and the part are separated. They can have their own lifecycles. Some can belong to multiple overall objects or share multiple overall objects. For example, the relationship between the computer and the CPU, the company and its employees. For example, an aircraft carrier Formation includes a sea-air ing ship, a escort ship, a carrier-based aircraft, and a nuclear-powered attack submarine. It is represented at the code level and is consistent with the association. It can only be distinguished at the semantic level. In the UML class diagram design, the aggregation relationship is represented by a hollow diamond plus a solid arrow.

Vi. Composite relationship

   A combination is also a special case of association. It represents a contains-a relationship, which is stronger than aggregation, also known as strong aggregation. It also reflects the relationship between the whole and the part, but the whole and the part are inseparable at this time. The end of the overall life cycle means that part of the life cycle ends, such as the human and human brain. It is represented at the code level and is consistent with the association. It can only be distinguished at the semantic level. In the UML class diagram design, the composite relationship is represented by a solid diamond plus a solid arrow.

VII. Summary

   There is no doubt about the relationship between inheritance and implementation. They reflect the vertical relationship between a class and a class, or between a class and an interface. The other four relationships reflect the reference and horizontal relationships between classes and classes, or between classes and interfaces, which is difficult to distinguish, it is difficult to accurately locate the relationship between many things. As mentioned above, these four relationships are semantic-level, so they cannot be fully differentiated from each other at the code level.
In general, the strengths and weaknesses of the subsequent links are: Combination> aggregation> association> dependency.
Supplement: associations can be bidirectional and unidirectional. The two-way direction can be with or without arrows. One-way with an arrow to become the link direction. Single items are more common, and two-way use is not encouraged. No need to mark the number of dependencies.

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.