Java thousand ask _05 object-oriented (006) _IS-A,HAS-A,LIKE-A is what

Source: Internet
Author: User

Click to enter _ many other _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 such as the following three kinds:
Is-a, Has-a, like-a
Java has a lot of representation 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 a. Represents an inheritance relationship.


Suppose a is-a B, then B is the parent class of a.
A class completely includes all the properties and behaviors of a class.
For example, a PC is a computer, a workstation is a computer, a PC and a workstation are two different kinds of computers. But they all inherit the common features of computers. Therefore, when implemented in the Java language, the PC and workstation should be defined as two kinds, all inheriting computer classes.
Learn a lot about other inheritance see here: what is the meaning of Java class inheritance?

3. What is HAS-A?

Has-a, as the name implies. There is one. Represents a subordinate relationship.
Assuming A has a B, then B is part of a.
Objects of the same kind are differentiated by different values of their properties.


For example, the operating system of a PC is windows, and the operating system of a PC is Linux.

The operating system is a member variable of the PC, which can distinguish different PC objects according to the different values of this member variable.

4. What is LIKE-A?

Like-a. Name implies. Like a, representing a combinatorial relationship.
Suppose a like a B, then B is the interface of a.
The new type has an old type of interface, but it also includes other functions, so it cannot be said that they are all the same.
For example, a mobile phone can be said to be a microcomputer, but the mobile phone communication function is obviously not the computer has the behavior, so the mobile phone inherits the characteristics of the computer, the same time need to achieve communication functions. Communication functions are required as separate interfaces. Rather than the behavior of the computer.

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

Assuming you are sure that the two-piece object is a is-a relationship, you should use inheritance at this point. For example, a diamond, a circle, and a square are all shapes. Then they should all inherit from the shape class.


If you are sure that the two-piece object is a has-a relationship, you should use aggregations at this point. For example, the computer is composed of display, CPU, hard disk and so on. Then you should aggregate the monitors, CPUs, and hard disks into computer classes.
Let's say you're sure that there's a like-a relationship between the two objects, you should use a combination at this point. For example, the air conditioner inherits from the refrigerator, but it has the heating function at the same time. Then you should let the air conditioner inherit the refrigeration class and realize the heating interface.

Java thousand ask _05 object-oriented (006) _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.