简体   繁体   中英

Aggregation and Compostion relationship in class diagram

i want to know that when there is an aggregation or composition relation between the two classes,can both classes share attributes of each other?

if there is a class called account which has a composition relation with class customer so is class account can access the variables of class customer?

Yes. Class can access the non-private fields of related classes, may it be aggregation or composition.

In the below example, class A & B holds a composition relationship and A can access fields and methods of B. But this depends on the visibility of the variable and method.

class A {
String test;
B b;
void printB(){
b.getTest();}
}

class B {
private String test;
public String getTest(){
return test;
}
}

It is not importent if two classes are connected with relationship or not. Attributes are accessible to any element if visibility of attribute is set to public (or package if elemenents reside in the same package). For example you can access public attribute of class in activity action which pin type is class with public attributes etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM