Four. Java inheritance and polymorphism 9. The relationship between classes and classes

Source: Internet
Author: User

There are three main types of relationships between classes and classes: Dependency (uses-a), Aggregation (has-a), and Inheritance (IS-A).

The following is an example of an online bookstore order system, which describes the concepts of these three relationships in detail.

The main function of the online bookstore Order system is: Registered users can log in to the online bookstore to buy books, fill orders online, and pay for the purchase of books. When the bookstore confirms that it has received the purchase, the book is mailed at the address left by the user. You can create several classes in this system, including books, accounts, orders, addresses, and so on, as shown in:

Dependency (USES-A)

A dependency is the most common relationship in a class, such as order, which requires access to the user account class, so the order class needs to refer to the account class, i.e. the order class is dependent on the account class, but the book class does not need to rely on the account class www.uuweb.cn.

If you modify the account class, the order class will be affected. The essence of dependency is that a method in a class can manipulate an instance of another class. In practical programming, it is recommended to minimize the number of interdependent classes. As shown in the following:

Aggregation (HAS-A)

Because the order needs to indicate what book to order, this involves the book class, that is, contains the book class. The difference between aggregation and dependency is that the order class can not have all the account class objects, but must have all the book class objects, because the book class object is the primary purpose of the order, as shown in:

Inherited

Inheritance is where a class can invoke all the data members of another class and does not need to be redefined in the current class, as the chapter has made clear.

Four. Java inheritance and polymorphism 9. The relationship between classes and classes

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.