Relationship between classes of "47" Java: Generalization, dependency, association, implementation, aggregation, composition

Source: Internet
Author: User

Relationships between classes in Java: Generalization, dependency, association, implementation, aggregation, combinatorial generalization:

? The generalization relationship (generalization), also known as the "is-a-kind-of" relationship, is used to describe the relationship between the parent class and the subclass, which is also referred to as the base class or superclass, which is also known as a derived class. In UML, the generalization relation is represented by a straight line with a hollow triangle.
? When the code is implemented, the generalization relationship is implemented using an object-oriented inheritance mechanism, such as the use of the extends keyword in the Java language, and the use of the colon ":" in c++/c#.

Generalization corresponds to the inheritance relationship in Java, that is, subclasses inherit everything outside the private adornment in the parent class (variables, methods, etc.). Example code:

publicclass Animal {}publicclass Tiger extends Animal {}
In UML, there are three requirements for generalization relationships:

1, the subclass and the parent class should be exactly the same, the parent class has the properties, operations, subclasses should have;

2. In addition to the information that is consistent with the parent class, the subclass includes additional information;

3. Where instances of the parent class can be used, instances of subclasses can also be used;

Depend on:

? A dependency relationship (Dependency) is a usage relationship in which a change in a particular thing can affect other things that use the thing, and use dependencies when it is necessary to indicate that one thing is using another. In most cases, a dependency is embodied in a method of a class that uses an object from another class as a parameter.
? In UML, a dependency relationship is indicated by a dashed line with arrows, and the relying party points to the relying party.

publicclass Screwdriver {    //螺丝刀,作为人类的工具,是用来被人类使用的}publicclass Person{       publicvoidscrew(Screwdriver src){    //拧螺丝,需使用螺丝刀}}
There are three types of dependencies:

1. Class A is a local variable in class B (a method).

2. Class A is a parameter of class B method;

3. Class A sends messages to Class B, which affects the change of Class B;

Association (Association):

is a structural relationship that shows that the object of one thing is associated with the object of another thing. Given the two classes associated, you can get an object from another class from an object of one class. The association has a two-yuan relationship and a multivariate relationship. Two USD relationship refers to a one-to-one relationship, and the relationship between multiple and multiple pairs. A simple association between two classes represents the structural relationship between the two equal-status classes. Use associations when you want to represent a structured relationship.
? An Association relationship (association) is the most common relationship between classes and classes, and is a structured relationship that is used to indicate that a class of objects is associated with another type of object.
? In a UML class diagram, a solid line is used to connect the classes of the associated objects, and when implementing an association relationship using programming languages such as Java, C #, and C + +, the object of one class is typically used as a property of another class.
? You can label a role name on an association line when you use a class diagram to represent an association relationship.

There are several situations: two-way correlation
publicclass Customer  {      private Product[] products;      ……  }  publicclass Product  {      private Customer customer;      ……  }  
One-Way Association
publicclass Customer  {      private Address address;      ……  }  publicclass Address  {      ……  }  
Self-correlating
publicclass Node  {      private Node subNode;      ……  
Aggregation Relationship (Aggregation)

? An aggregation relationship (Aggregation) represents a whole-to-part relationship. Usually after defining a whole class, we analyze the constituent structure of the whole class, and find out some member classes, which form the aggregation relationship between the whole class and the member class.
? In an aggregation relationship, the member class is part of the whole class, that is, the member object is part of the overall object, but the member object can exist independently from the overall object. In UML, the aggregation relationship is represented by a straight line with a hollow diamond.

publicclass Student{}publicclass School{         private List<Student> students ;  //学校与学生是聚合关系}
Example:

Composition relationship (composition)

The combined relationship (composition) also represents the relationship between the whole and part of the class, but the part and the whole of the composite relationship have a uniform lifetime. Once the whole object does not exist, some objects will not exist, and some objects have a die relationship with the whole object.
? In a composite relationship, the member class is part of the whole class, and the whole class can control the life cycle of the member class, that is, the existence of the member class depends on the whole class. In UML, a composite relationship is represented by a straight line with a solid diamond.

publicclass Menu{}publicclass Window{       private List<Menu> menus ;}

The existence of a menu is premised on the existence of a window, which has a strong relationship of ownership. The window is responsible for the life cycle of the menu, and the menu is created only after the window is created, and the menu must be destroyed before the window is destroyed. So the window and menu are a combination of relationships.

Implementation relationship (Implementation)

? Interfaces can also have inheritance relationships and dependencies similar to those between classes, but there is also an implementation relationship between the interface and the class (realization), in which the class implements the interface, and the operations in the class implement the actions declared in the interface. In UML, the implementation relationship between a class and an interface is represented by a dashed line with a hollow triangle.

 Public  interface Vehicle {       Public void Move(); } Public  class  ship implements Vehicle {       Public void Move() { ...... } } Public  class Car implements Vehicle {       Public void Move() { ...... } }
Summarize:

There is no significant difference between associations, aggregations, and combinations in implementation, and they differ only by judging the actual relationship between the two parties, such as the relationship between the strengths and weaknesses, the creation and destruction of the connection, etc.
There is no objection to their strong and weak relationship: Dependency < Association < aggregation < combination < generalization (inheritance)

How to implement the difference:

1) Dependency: A Relational object appears in the parameters of a local variable or method, or a static method of a relationship class is called
(2) Association relationship: The relationship object appears in the instance variable
(3) Aggregation relationship: The Relationship object appears in the instance variable
(4) Composition relationship: The Relationship object appears in the instance variable
(5) Generalization:extends
(6) Realization: Implements

Welcome to the group: public number It face questions summary discussion group

If the scan does not go in, add me (rdst6029930) pull you. Sweep my QR code plus me

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

Reference blog:

http://blog.csdn.net/vickyway/article/details/7583545

http://blog.csdn.net/hguisu/article/details/7609483

Relationship between classes of "47" Java: Generalization, dependency, association, implementation, aggregation, composition

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.