Briefly talk about the relationship between Java classes and classes _java

Source: Internet
Author: User
Tags inheritance

The most common relationships between classes and classes are mainly three: dependencies (uses-a), aggregations (has-a), and Inheritance (IS-A).

The following is an example of an online bookstore order system that details the concepts of these three relationships.

The main functions of the online bookstore Order system are: Registered users can login to the online bookstore to buy books, fill out the order online, and pay the purchase book. When the bookstore confirms that the book has been received, mail the book at the address left by the user. You can create several classes in this system, books (book), accounts (account), orders (order), addresses (address), and so on, as shown in the following illustration:

Dependence (USES-A)

Dependencies are the most common relationships in a class, such as order classes (orders) that require access to user account classes (account), so it is necessary to reference the accounts class in the order class, i.e. the order class relies on the account class, but the book class does not need to rely on account classes.

If you modify the account class, it will affect the order class. The essence of dependency is that methods in a class can manipulate instances of another class. In practical programming, it is recommended to minimize the number of interdependent classes. As shown in the following illustration:

Aggregation (HAS-A)

Because orders need to indicate what books to order, this involves books, that is, the book category. The difference between aggregation and dependency is that the order class can not have all the account class objects, but must have all the library objects, because the object of the book class is the main purpose of the order, as shown in the following illustration:

Inherited

Inheritance is a class that can call all data members of another class, and it does not need to be redefined in the current class, and this chapter is clear.

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.