Java Technology _java thousand questions (0034) _is-a,has-a,like-a is what

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is IS-A,HAS-A,LIKE-A?

In the field of object-oriented design, there are several kinds of design ideas, mainly the following three kinds:
Is-a, Has-a, like-a
In Java, there are many manifests in classes, interfaces, and abstract classes.
Read about Java here: what is Java
Learn about classes and objects look here: What is the secret of classes and objects?
Learn about interfaces and abstract classes here: What is the difference between an interface and an abstract class?

2. What is Is-a?

Is-a, as the name implies, is one that represents the inheritance relationship.
If a is-a B, then B is the parent class of a.
A class completely contains all the properties and behaviors of another class.
For example, a PC is a computer, a workstation is a computer, a PC and a workstation are two different types of computers, but all inherit the common features of a computer. Therefore, when implemented in the Java language, the PC and workstation should be defined as two kinds, all inheriting computer classes.

3. What is HAS-A?

Has-a, as the name implies, has one that represents a subordinate relationship.
If A has a B, then B is part of a.
Objects of the same kind are distinguished by different values of their properties.
For example, one PC's operating system is windows and the other PC's operating system is Linux. The operating system is a member variable of the PC, according to the different values of this member variable, can distinguish different PC objects.

4. What is LIKE-A?

Like-a, as the name implies, represents a combination of relationships.
If a like a B, then B is the interface of a.
The new type has an old type of interface, but it also contains other functions, so it cannot be said that they are identical.
For example, a mobile phone can be said to be a microcomputer, but the communication function of mobile phone is obviously not the behavior of the computer, so the mobile phone inherits the characteristics of the computer, but also need to realize the communication function, and communication function needs as a separate interface, not the behavior of the computer.

5, is-a,has-a,like-a How to apply

If you are certain that the two objects are is-a, then you should use inheritance at this point, such as diamonds, circles, and squares are all shapes, and they should all inherit from the shape class.
If you are certain that the two objects are has-a, then you should use aggregations at this point, such as the computer is composed of monitors, CPUs, hard disks, etc., then you should aggregate the monitors, CPUs, hard disks into computer classes.
If you are certain that the two objects are like-a, then you should use a combination at this point, such as the air conditioner inherits from the refrigerator, but it also has the heating function, then you should let the air conditioner inherit the refrigerator class, and realize the heating interface.

Java Technology _java thousand questions (0034) _is-a,has-a,like-a is what

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.