Java Fundamentals--inheritance, implementation, dependency, association, aggregation, combination of connections and differences

Source: Internet
Author: User

Inherited

Refers to a class or interface that inherits another class or interface and can add its own new functionality.

Realize

Refers to a class that implements the interface interface.

Dependent

Simply put, the method in one class uses another class, and the general dependency in Java is either a local variable, a formal parameter to a method, or a call to a static method. is a weak relationship. For example, when a person writes with a pen, the relationship between a person and a pen is dependent.

Associate

An association is a strong relationship between a class and a class, or a strong dependency between a class and an interface. Generally in the form of class attributes in the association class, in Java Association Relationships are generally implemented using member variables.

Aggregation

Aggregation is a special case of association, which embodies the relationship between the whole and the part, that is has-a.

Has-a is not must has, such as airports and airplanes, airports can be parked aircraft, or can not stop the plane, the aircraft can be parked in the airport, can also fly in the sky.

They have their own life cycle.

Combination

Combination is also a special case of association, he embodies the relationship between the contains-a, more than the aggregation.

The combination also embodies the whole and part of the relationship, but the whole and part are indivisible, they have a common life cycle.

For example, if a person has an ear and a nose, he or she will die.

There should be no doubt about inheritance and implementation, they embody a vertical relationship between classes and classes, classes, and interfaces directly. The other four shows the class and class, the reference between the class and the interface, and the horizontal relationship, which is difficult to distinguish. There is not much difference in the code, mainly the semantic difference.

The strength of several relationships is: Combination > Aggregation > Association > dependency.

Aggregation and composition

    1. Aggregation and composition are an association relationship, but with a different whole-part meaning.
    2. Part of the whole part has a different life cycle. Aggregation relationship, the whole does not have a part of the life cycle, the overall deletion, the part will not be deleted, multiple whole share the same part. In a combined relationship, the whole has part of the life cycle, and when the whole is deleted, the part is bound to be deleted.

Also, multiple pieces can not share the same part at the same time. (to distinguish whether an association is a combination or an aggregation, to see if the life cycle of two classes is synchronized, synchronization is a composite relationship, and the other is the aggregation relationship)

3. Aggregation is a has-a relationship, and a combination is a contains-a relationship.

To give a simple example:

We need to go to work in the company, so a lot of employees formed the company, each company has its own employees, each employee also has its own company. Employees and companies are aggregated relationships. In addition, each employee has hands and feet, the relationship between man and foot is a combination, inseparable, with the same life cycle.

Aggregation diagram:

  

Composition diagram:

The code looks different:

 Public class Company {    public list<company> companylist;       Public Company (list<company> companylist) {        this. companylist = companylist;    }} 
 Public class Employee {    public  Hand Hand;      Public Employee () {        thisnew  Hand ();    }}

The encapsulation of information is different
In an aggregation relationship, callers can understand both the company and the employee class, because they are independent
In the combinatorial relationship, the caller only knows the employees and does not know the existence of the hand, because the hand class is tightly encapsulated in the employee class.

http://blog.csdn.net/qq_31655965/article/details/54645220

Java Fundamentals--inheritance, implementation, dependency, association, aggregation, combination of connections and differences

Related Article

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.