Java technology insider _ What is the relationship between is-a and has-

Source: Internet
Author: User

He is a people

He has a head

 

I am a man is-

I have a woman, has-.

 

Is_a is used to inherit

Has_a is a combination of classes that contain other classes.

 

 

We generally think that inheritance can be divided into two basic forms: Implementation inheritance and interface inheritance.

 

The main objective of inheritance isCodeReuse:

 

We found that class B and class C have the same code, so we designed Class A to store common code.

 

Inheritance based on this idea is called implementation inheritance.

Different interfaces inherit from each other. They are based on the semantics in real life and represent the relationship between ISA:

 

We believe that both the deposit account and settlement account are sub-classes of accounts. This inheritance is called interface inheritance.

 

Note that someArticleThe behavior of a class to implement an interface is defined as interface inheritance, which is different from the interface inheritance here. to distinguish between the two concepts, we can use another name inherited by the interface-type inheritance.

 

The key to inheritance lies in the flexible use of the two inheritance methods.

The meaning of the interface here is different from that in Java or C.

 

This interface indicates the signature of all public methods in a class.

So the ISA relationship is about behavior.

 

 

Hasa relationship, also known as object combination. In fact, there are two hasa relationships.

First, the static hasa relationship. This is called composition in UML, for example, "automobile has-a engine ".

This relationship is similar to aggregation, except that the life cycle of the 'part' is controlled by the 'holistic.

 

Such control may be direct or transmitted. That is, the 'holistic 'may have direct responsibilities for creating or destroying the 'part', or it may obtain the created part, which will be passed to other assumptions that have responsibilities for it later.

Second, dynamic hasa relationship. This is called aggregation in UML, for example, "airport has-a aircraft ".

 

 

 

Don't forget that use-A is a dependency.

 

 

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.