{
    "componentChunkName": "component---src-templates-docs-js",
    "path": "/old/02-theory/design_class_relations",
    "result": {"data":{"site":{"siteMetadata":{"title":"Linnéuniversitetet","language":"sv","courseCode":"1DV607"}},"mdx":{"fields":{"id":"a0e8605b-7c48-52f5-9ebe-8fef7a1e5571","title":"UML Design Class Relations","slug":"/old/02-theory/design_class_relations"},"body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"UML Design Class Relations\",\n  \"metaTitle\": \"UML Notation for Class Relations with Java Examples\",\n  \"metaDescription\": \"UML Dependency, Association, Generalization and Realization Notation with Java Examples\"\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar Youtube = makeShortcode(\"Youtube\");\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"During the execution of a program objects collaborate in different ways. This collaboration is most often different kinds of messages sent between objects, e.g. method calls. To be able to send a message from one object to another there needs to be a link between the objects, i.e. the sending object needs to know about the recieving object in some way. This link can be formed in two basic ways, ether the sending object contains an attribute that holds the recieving object, or the recieving object is known as an argument or local variable. A local variable object can either be created, or recieved as a return value.\"), mdx(\"p\", null, \"In a class diagram we show all the possible links an object of a class can have, in the form of associations and depdenencies. In addition we can show if a class inherits another class, or if it implements an interface.\"), mdx(\"p\", null, \"This gives us four basic relations between classes in UML: \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"dependency, association, generalisation\"), \" and \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"realisation\"), \". The dependency is the most generic of the four and if none of the others apply a dependency is often used.\"), mdx(\"p\", null, \"Using the relations in a class diagram we can reason about properties such as reusability, order of implementation, complexity, maintainability. For example, a class with many outgoing relations can be considered fragile and complicated, it is aware of many other classes and could thus be impacted by change in any of these. Reusing such a class is often hard as you would need to either remove many relations, or bring along many other classes when reusing. On the other hand a class with many incoming relations can be hard to change without breaking the whole system e.g. it is a hot-spot that could benefit from further division.\"), mdx(\"p\", null, \"In practical diagramming showing all relations in a full class diagram often creates a very messy diagram. While a mess can be a good visual cue for refactoring (\\\"this part looks too messy\\\") it is common to not show every single relation, especially dependencies. Good tooling is essential and can provide different perspectives and levels of detail.\"), mdx(\"h2\", null, \"Design Class Diagram Notation Crash Course\"), mdx(Youtube, {\n    link: \"https://www.youtube.com/embed/padlnp0QheI\",\n    mdxType: \"Youtube\"\n  }), mdx(\"h2\", null, \"Association\"), mdx(\"p\", null, \"An association is a persistens relation that can exist between two or more objects. That the relation is persistent means that it exists for some period of time, typically it persists between multiple method calls. As such the associations define the major structural relations. In design level class diagrams the association has \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"navigability\"), \" (direction), \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"multiplicity\"), \" and a \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"role\"), \" name, the association name is \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"not\"), \" used.\"), mdx(\"p\", null, \"In practice the multiplicity 0..1 are often not shown explicitly in a diagram. E.g a car can be without a driver, but never without a registration number.\"), mdx(\"h3\", null, \"UML\"), mdx(\"p\", null, \"In source code an association is often implemented using an attribute, we do \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"not\"), \" typically show this attribute in the class diagram as it would be redundant.\\n\", mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"501px\"\n    }\n  }, \"\\n      \", mdx(\"a\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-link\",\n    \"href\": \"/courses/object-oriented-analysis-and-design/static/bf11eb054169cac59c61417bf7b57694/55811/uml_design_associations.png\",\n    \"style\": {\n      \"display\": \"block\"\n    },\n    \"target\": \"_blank\",\n    \"rel\": \"noopener\"\n  }, \"\\n    \", mdx(\"span\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"108.1081081081081%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAIAAABPIytRAAAACXBIWXMAABYlAAAWJQFJUiTwAAACFElEQVQ4y52U266iMBRA+f+f8dVoYkII4gXDRRI1XgCLiQLaFiqlXOwkNHKYczhOMuuhbcpe7La7IFFKswZKaZIkZVnyDs/ns31KKWWMiQEhhFIqIYRkWZ7NZsvl0nEcxhjn/PV6ifZyuSwWC13XVVWdz+fD4VDTtPl8bpomhFDCGBuGsWzYbrdVVbVpX6/X9XqdzWaGYUynU13XNU2TZXmxWGy3W4SQFEXR6XTyPA8A4DhOFEVtZs55EARew/l8Fq3geDxGUSRVVUUIuVwuZVnu93tVVbsyY4wQInYehiFCKMuyZ0NVVZIIhRCKaADA4XBo5S4QwrquuzOS6O73e7tbSilCqN22eBEhpJ38LjPG4jiu30AIuzXDGLdL65GF/3g8AAAilFLKOS/LEiEkxp9ksTy94Xa7xXGsKIrv+wghcSW6VeyXKaWe500mE9u2XdfNsgxCGMdxmqaf5JYgCGzb3mw24oR6T/5XOY7j8/kMAEjTtHvg/5ZFHMZYXPLPSL2z/ymLzEmSMMbqui7LsiiKn0f1SQ7DMEmSIAgAAK7riq/lVznPc8ZYURR5nnPONU3zfV9ckrqu8zwviqJHFqlOp9NqtbIsyzAMy7LW67X9xjTNzWaDMf5Zti95NBoNBgOlQX6jKMp4PDYMgxDSI4vOdV3P81zX9X3fe+M3AAB2u923n8RfcpqmEEKMMeoDQthbuT86r+PfE3EJ2gAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), \"\\n  \", mdx(\"img\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"alt\": \"UML Design Associations\",\n    \"title\": \"UML Design Associations\",\n    \"src\": \"/courses/object-oriented-analysis-and-design/static/bf11eb054169cac59c61417bf7b57694/55811/uml_design_associations.png\",\n    \"srcSet\": [\"/courses/object-oriented-analysis-and-design/static/bf11eb054169cac59c61417bf7b57694/a2ead/uml_design_associations.png 259w\", \"/courses/object-oriented-analysis-and-design/static/bf11eb054169cac59c61417bf7b57694/55811/uml_design_associations.png 501w\"],\n    \"sizes\": \"(max-width: 501px) 100vw, 501px\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    },\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\"\n  }), \"\\n  \"), \"\\n    \")), mdx(\"h3\", null, \"Java\"), mdx(\"p\", null, \"As associations are persistent they must be implemented using fields in Java. The association role name corresponds to the implementing field name. In practice the only multiplicities we can enforce syntactically are 0..1 and 0..* E.g. having a field that can contain 0 or 1 object, or a field that can contain 0 or many objects. Other mutliplicites needs to be enforced by writing code for example by adding appropriate constructors.\"), mdx(\"p\", null, \"It may also be the case that for practial reasons you allow the 0..* multiplicity in the code while the design specifies another multiplicity. I.e. you first create an empty object and then add some other objects to is via special methods.\"), mdx(\"p\", null, \"Typically the design does not dictate how multiplcity \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"*\"), \" should be implemented.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-java\"\n  }, \"\\nimport java.util.List;\\n\\nclass RegistrationNumber {\\n    \\n}\\n\\nclass Person {\\n    private List<Car> owns; // a list can contain 0 or many Car objects\\n}\\n\\nclass Car {\\n    private Person driver; \\n    private RegistrationNumber registeredBy;\\n    private Person owner;\\n\\n    // There must be one RegistrationNumber associated to a Car object\\n    public Car(RegistrationNumber registeredBy) {\\n        assert(registeredBy != null);   // error if there is no registration number to enforce the 1 multiplicity\\n        this.registeredBy = registeredBy;\\n    }\\n}\\n\\npublic class AccidentReport {\\n    private Car[] involves; // an array can contain 0 or may Car objects\\n\\n    // There must be at least one Car object associated to an AccidentReport object\\n    public AccidentReport(Car theCar) {\\n        assert(theCar != null);         // enforce that there is a valid Car object\\n        involves = new Car[]{theCar};\\n    }\\n\\n    public AccidentReport(Car[] involvedCars) {\\n        // we should check this for null etc. but this ie beyond the scope of the example\\n        involves = involvedCars;\\n    }\\n}\\n\\n\")), mdx(\"h2\", null, \"Dependency\"), mdx(\"p\", null, \"In a design class diagram the \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"dependency\"), \" relation means that one class needs another class to be able to work (e.g. A cannot be compiled without B). If we need a relation but we cannot refine it to some other more specific relation (\", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"association\"), \", \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"realization\"), \" or \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"generalization\"), \") dependency should be used. Additional dependencies are typcially not added if we already have some other relation. Typically we also do not show dependencies from a class if the dependency is already in a more general class or an interface.\"), mdx(\"p\", null, \"In general we can find the dependency relation in many different diagrams, and also it is often used in general software discussions.\"), mdx(\"p\", null, \"In some cases the dependency has a stereotype that explains what the dependency is caused by. For example \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"creates\"), \" that indicats that objects are created.\"), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"665px\"\n    }\n  }, \"\\n      \", mdx(\"a\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-link\",\n    \"href\": \"/courses/object-oriented-analysis-and-design/static/48d623f4c4d8947a412af3bf2d67a795/5f4af/uml_design_dependency.png\",\n    \"style\": {\n      \"display\": \"block\"\n    },\n    \"target\": \"_blank\",\n    \"rel\": \"noopener\"\n  }, \"\\n    \", mdx(\"span\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"52.50965250965252%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAABYlAAAWJQFJUiTwAAABI0lEQVQoz3VS26rCMBDM/3+YCPqigUJpvKF4qWJo01y2u2vknBxCqMd5HGYyO7sR8QOv1yvGaIzZ7/d932fmEyJ+gdZ6uVxqreN3iM9YAEDE8/m8Wq1OpxMiAsC/4WJic87RL6y1xhhrLSKO4+i9BwBmnpqZGQBCCESktb5er8fj0VqbFN57IooxElGW/ZmJKKWl8BBC13V934/jmBTMHELIaczsvUfEH7MxpuzDzM65STdr7WRgYwwiivV6nTLTE13XzWazPFjCfD5/Pp+lrKqqpmlEXdfleR+Ph1IqFU4MAEgp27YtD66U2m63QkpZmtu2lVIOw5CZEMJisbhcLqW5qqq6rsVut8vbijHe73elVPmxnHNN09xut8wQ0eFw2Gw2b2qLfl+EmL2sAAAAAElFTkSuQmCC')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), \"\\n  \", mdx(\"img\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"alt\": \"UML Design Dependency\",\n    \"title\": \"UML Design Dependency\",\n    \"src\": \"/courses/object-oriented-analysis-and-design/static/48d623f4c4d8947a412af3bf2d67a795/5f4af/uml_design_dependency.png\",\n    \"srcSet\": [\"/courses/object-oriented-analysis-and-design/static/48d623f4c4d8947a412af3bf2d67a795/a2ead/uml_design_dependency.png 259w\", \"/courses/object-oriented-analysis-and-design/static/48d623f4c4d8947a412af3bf2d67a795/6b9fd/uml_design_dependency.png 518w\", \"/courses/object-oriented-analysis-and-design/static/48d623f4c4d8947a412af3bf2d67a795/5f4af/uml_design_dependency.png 665w\"],\n    \"sizes\": \"(max-width: 665px) 100vw, 665px\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    },\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\"\n  }), \"\\n  \"), \"\\n    \")), mdx(\"h3\", null, \"Java\"), mdx(\"p\", null, \"In Java dependenices are formed by having \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"local variables\"), \", \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"arguments in methods\"), \", \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"return types\"), \" or \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"calling static methods\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-java\"\n  }, \"\\nclass A {\\n\\n    // dependency to C as it is the return type\\n    // dependency to B as it is an argument\\n    public C theOperation(B theBObject) {\\n        D aLocalVariable;           // dependency to D as it is the type of a local variable \\n        E.callStaticOperation();    // dependency to E as we call a static operation in that class\\n        return new C();             // dependeny to C as its constructor is called\\n    }\\n}\\n\\nclass B {\\n\\n}\\n\\nclass C {\\n\\n}\\n\\nclass D {\\n\\n}\\n\\nclass E {\\n    public static void callStaticOperation() {\\n\\n    }\\n}\\n\\n\\n\")), mdx(\"h2\", null, \"Realisation and Generalisation\"), mdx(\"p\", null, \"Realisation describes the fulfillment of e.g. an interface. Generalisation describes that e.g. a class is part of a generalisation/specialisation hierarchy. For \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"realization\"), \" the lollipop notation is also available, but it is often more common in  component diagrams.\"), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"884px\"\n    }\n  }, \"\\n      \", mdx(\"a\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-link\",\n    \"href\": \"/courses/object-oriented-analysis-and-design/static/da31074dffa6045b6bc922dc7028e27f/136a2/uml_design_generalisation_realisation.png\",\n    \"style\": {\n      \"display\": \"block\"\n    },\n    \"target\": \"_blank\",\n    \"rel\": \"noopener\"\n  }, \"\\n    \", mdx(\"span\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"43.24324324324324%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABJ0lEQVQoz5WRTY/CIBRF+f//p3Fh0k2DUONCV2qxKiR+UKlPaG1qtb6ZlEjM7OasOOTehFyI1toYUxSFGjifz8YYrXVVVYj4fr8RsW3bsizhw+12A4CqqsjlcgGA2Ww2Go2iKBqPx0VRlGXpnAtlY0wcx0mS0IFkYLPZECmlUirLMiFEnufL5VJKud/vASCUr9frdrtVSkkp/WG32x0OB9I0TV3XXdedTqfpdNr3fT3QdR1+0fc9IuZ5zhgLSr7ftlgsgv7BX1prtdZBCf6H1+vVtm1Q4pyzH/xOQZ/Ppw89Hg9rrXMOAIwxbuB+vxPGGOc8TVPOOaWUD1BK5/N50zS+bK2dTCYhwBhL0/R4PBK/sxAi+xDUfzUiAsBqtQoZIcR6vf5d+wfb8u8lpZVMAQAAAABJRU5ErkJggg==')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), \"\\n  \", mdx(\"img\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"alt\": \"UML Design Realisation and Generalisation\",\n    \"title\": \"UML Design Realisation and Generalisation\",\n    \"src\": \"/courses/object-oriented-analysis-and-design/static/da31074dffa6045b6bc922dc7028e27f/136a2/uml_design_generalisation_realisation.png\",\n    \"srcSet\": [\"/courses/object-oriented-analysis-and-design/static/da31074dffa6045b6bc922dc7028e27f/a2ead/uml_design_generalisation_realisation.png 259w\", \"/courses/object-oriented-analysis-and-design/static/da31074dffa6045b6bc922dc7028e27f/6b9fd/uml_design_generalisation_realisation.png 518w\", \"/courses/object-oriented-analysis-and-design/static/da31074dffa6045b6bc922dc7028e27f/136a2/uml_design_generalisation_realisation.png 884w\"],\n    \"sizes\": \"(max-width: 884px) 100vw, 884px\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    },\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\"\n  }), \"\\n  \"), \"\\n    \")), mdx(\"h3\", null, \"Java\"), mdx(\"p\", null, \"In Java \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"generalisation\"), \" is implemented using \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"inheritance\"), \" i.e. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"extends\"), \", and \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"realisation\"), \" is implemented using \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"implements\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-java\"\n  }, \"\\ninterface AnInterface {\\n\\n}\\n\\nclass AnInterfaceImplementation implements AnInterface {\\n\\n}\\n\\nclass AMoreGeneralClass {\\n\\n}\\n\\nclass AMoreSpecialClass extends AMoreGeneralClass {\\n\\n}\\n\\n\")), mdx(\"h2\", null, \"Composition and Aggregation\"), mdx(\"p\", null, \"Composition and aggregation are special types of assocaiations. Composition indicates a strong whole part relationship where the part cannot be removed from the whole. I.e. the part lives and dies with the whole. For example a hand is composed of a palm and five fingers. In some cases also the parts cannot be shared with other objects, e.g. a finger cannot belong to two hands.\"), mdx(\"p\", null, \"Aggregation indicates a loose whole part relationship where the parts can live without being part of a whole, in some cases the parts can also be shared by multiple whole objects at the same time. We can think of for example a car that has four tires, the tires can be removed from the car and put on another car, however they cannot be part of two different cars at the same time.\"), mdx(\"p\", null, \"The exact interpretation of composition and aggregation is debated for design class diagrams, esp. their interpretation and implementation in garbage collected languages. Typically composition can be useful in multithreaded applications where sharing objects can be problematic, also composition can be used to indicate how objects should be copied. E.g. if a hand object is to be copied, the copy cannot share the finger objects between them. Howeve, this interpretaion of composition and aggregation is not considered standard among developers.\"), mdx(\"p\", null, \"When interpreting a diagram what contains composition and/or aggregation the standard interpretation is to consider them to be associations. Exactly, what is meant by the composition/aggregation must be made clear on a case to case basis.\"), mdx(\"p\", null, \"Ther rule of thumb is to not spend too much time on debating compositon v.s. aggregation but rather use association, esp. if the design is to be implemented in a single threaded garbage collected language.\"), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"441px\"\n    }\n  }, \"\\n      \", mdx(\"a\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-link\",\n    \"href\": \"/courses/object-oriented-analysis-and-design/static/e1bb1e558de1020e694894d81ed8a87f/efc6e/uml_design_composition_aggregation.png\",\n    \"style\": {\n      \"display\": \"block\"\n    },\n    \"target\": \"_blank\",\n    \"rel\": \"noopener\"\n  }, \"\\n    \", mdx(\"span\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"79.92277992277992%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB10lEQVQoz32SS6+qMBSF/f/+hZM4J3HiSAZXYzQqGhANKgG5tRQilPIoCLGRx8k5GPSKuWtS0r2+rr1LO0mSEELiOI6iyPf9KIqqlrIs8zyPUkoICYKg2e/EcawoymQy2W63giBgjKuqKsuyLtcfSZJomrZcLg+Hg+d5TzhJks1mw3Hcn1/VtQaulaapqqqDwWC1WoVh2FQ7hBAIoed5GGMI4eVyaSdHUQQhJIQghCzLeiZTSgEAuq4fj0cAwFvbta7XK0Jov99rmlYbHnBRFFVVcRzX7XbbWCOMsSiKtm2/eh4wz/NfX1//gQkhkiS93UinXm6328eGXyfHGN9ut9f9B8wYI4SUZVn8Ks/zNkwIYYwVRVHbyrJ8wnEch2EoSZKqqr7vt/MppYwxCKFhGJZlUUofM8uyPBwO68fEGPvYeb/fBwDUSXmeF0Xx85/TNIUQKoqSZZnruvVxb5lRFO12O9u2gyBoDB3TNKfT6WKxEEVREAQI4evA9bSUUkEQ5vO5oii2bTeGH3i9XvM83+v1RqMRQugjLMvyeDyezWaWZT2TIYSO47iua9u267qn0+l+v7/BrutCCDHGjuOcz+dnMkLo779qJ/u+r+s6AMAwDNM0m0fyDVulhD0NCJ7VAAAAAElFTkSuQmCC')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), \"\\n  \", mdx(\"img\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"alt\": \"UML Design Composition and Aggregation\",\n    \"title\": \"UML Design Composition and Aggregation\",\n    \"src\": \"/courses/object-oriented-analysis-and-design/static/e1bb1e558de1020e694894d81ed8a87f/efc6e/uml_design_composition_aggregation.png\",\n    \"srcSet\": [\"/courses/object-oriented-analysis-and-design/static/e1bb1e558de1020e694894d81ed8a87f/a2ead/uml_design_composition_aggregation.png 259w\", \"/courses/object-oriented-analysis-and-design/static/e1bb1e558de1020e694894d81ed8a87f/efc6e/uml_design_composition_aggregation.png 441w\"],\n    \"sizes\": \"(max-width: 441px) 100vw, 441px\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    },\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\"\n  }), \"\\n  \"), \"\\n    \")), mdx(\"h3\", null, \"Java\"), mdx(\"p\", null, \"Syntactically composition and aggregation are implemented as associations, i.e. using fields. There is no easy way to know if composition or aggregation is actually used by just inspecting Java code and it is more or less up to interpretation. Hence for Java code composition and aggregation are not that useful. Do not spend time on these concepts, just use the plain asociation.\"), mdx(\"p\", null, \"If aggregated values are \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"not\"), \" be shared the implementation is often rather tricky. This is just an example on how this could be achieved, but definitely not the only way.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-java\"\n  }, \"class Hand {\\n  Finger [] fingers;\\n\\n  Hand() {\\n    fingers = new Finger[5];\\n      for (int i = 0; i < fingers.length; i++) {\\n        fingers[i] = new Finger();\\n      }\\n    }\\n\\n  Hand(Hand toBeCopied) {\\n    // note that we do not share the finger objects, we copy them\\n    fingers = new Finger[5];          \\n    for (int i = 0; i < fingers.length; i++) {\\n      fingers[i] = new Finger(toBeCopied.finger[i]);\\n    }\\n  }\\n}\\n\\nclass Finger {\\n  Finger() {\\n\\n  }\\n\\n  Finger(Finger toBeCopied) {\\n\\n  }\\n}\\n\\n\\nclass Car {\\n  List<Tire> tires;\\n\\n  Car() {\\n    tires = new ArrayList<>();\\n  }\\n\\n  boolean install(Tire aTire) {\\n    if (tires.size() < 4 && aTire.install()) {\\n      tires.add(aTire);\\n      returrn true;\\n    }\\n    return false;\\n  }\\n\\n  boolean remove(Tire aTire) {\\n    ...\\n  }\\n}\\n\\nclass Tire {\\n  boolean isInstalled;\\n    \\n  Tire() {\\n    isInstalled = false;\\n  }\\n\\n  boolean install() {\\n    if (isInstalled) {\\n      // we should probably throw an exception here as\\n      // you are installing a tire that is already installed\\n      return false;\\n    }\\n      isInstalled = true;\\n      return true;\\n    }\\n\\n    void remove() {\\n      isInstalled = false;\\n    }\\n\\n    boolean isInstalled() {\\n      return isInstalled;\\n    }\\n}\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{"items":[{"url":"#design-class-diagram-notation-crash-course","title":"Design Class Diagram Notation Crash Course"},{"url":"#association","title":"Association","items":[{"url":"#uml","title":"UML"},{"url":"#java","title":"Java"}]},{"url":"#dependency","title":"Dependency","items":[{"url":"#java-1","title":"Java"}]},{"url":"#realisation-and-generalisation","title":"Realisation and Generalisation","items":[{"url":"#java-2","title":"Java"}]},{"url":"#composition-and-aggregation","title":"Composition and Aggregation","items":[{"url":"#java-3","title":"Java"}]}]},"parent":{"relativePath":"old/02-theory/design_class_relations.md"},"frontmatter":{"metaTitle":"UML Notation for Class Relations with Java Examples","metaDescription":"UML Dependency, Association, Generalization and Realization Notation with Java Examples","fullWidth":null}},"allMdx":{"edges":[{"node":{"fields":{"slug":"/forelasningar-2026","title":"Föreläsningar 2026"}}},{"node":{"fields":{"slug":"/","title":"Objektorienterad analys och design med UML (1DV607)"}}},{"node":{"fields":{"slug":"/laborationer-2026","title":"Laborationer 2026"}}},{"node":{"fields":{"slug":"/old","title":"Old"}}},{"node":{"fields":{"slug":"/forelasningar-2026/00-tema-2-objektorienterad-analys-och-uml","title":"Tema 2 – Objektorienterad analys och modellering med UML"}}},{"node":{"fields":{"slug":"/forelasningar-2026/00-tema-3-objektorienterad-design-med-uml","title":"Tema 3 – Objektorienterad design med UML"}}},{"node":{"fields":{"slug":"/forelasningar-2026/01-kursintroduktion-och-domanmodellering","title":"1. Kursintroduktion och domänmodellering"}}},{"node":{"fields":{"slug":"/forelasningar-2026/02-objektorienterad-analys-och-modellering","title":"2. Objektorienterad analys och modellering"}}},{"node":{"fields":{"slug":"/forelasningar-2026/03-uml-modellering","title":"3. Fortsatt modellering med UML"}}},{"node":{"fields":{"slug":"/forelasningar-2026/04-grasp","title":"4. GRASP"}}},{"node":{"fields":{"slug":"/forelasningar-2026/00-tema-1-grunderna","title":"Tema 1 – Grunderna i objektorienterad analys och design"}}},{"node":{"fields":{"slug":"/laborationer-2026/01-laboration-1","title":"Laboration 1 – Domänmodellering"}}},{"node":{"fields":{"slug":"/old/001-introduction","title":"Course Introduction"}}},{"node":{"fields":{"slug":"/old/01-information","title":"Course Information"}}},{"node":{"fields":{"slug":"/laborationer-2026/02-laboration-2","title":"Laboration 2"}}},{"node":{"fields":{"slug":"/old/02-theory","title":"Theory"}}},{"node":{"fields":{"slug":"/old/03-assignments","title":"Assignments"}}},{"node":{"fields":{"slug":"/old/01-information/01-welcome","title":"Welcome to the Course"}}},{"node":{"fields":{"slug":"/old/01-information/registration","title":"Course Registration"}}},{"node":{"fields":{"slug":"/old/02-theory/01-intro_to_ooad","title":"Introduction to Object Oriented Analysis and Design"}}},{"node":{"fields":{"slug":"/old/01-information/grading","title":"Course Grading"}}},{"node":{"fields":{"slug":"/old/02-theory/03_software_architecture","title":"Software Architecture"}}},{"node":{"fields":{"slug":"/old/02-theory/04_mvc","title":"Model View Controller"}}},{"node":{"fields":{"slug":"/old/02-theory/02-domain_modeling","title":"Domain Modeling"}}},{"node":{"fields":{"slug":"/old/02-theory/06_grasp1","title":"GRASP 1"}}},{"node":{"fields":{"slug":"/old/02-theory/05_layers_components","title":"Layers & Components"}}},{"node":{"fields":{"slug":"/old/02-theory/07_grasp2","title":"GRASP 2"}}},{"node":{"fields":{"slug":"/old/02-theory/09_gof1","title":"GoF 1"}}},{"node":{"fields":{"slug":"/old/02-theory/10_gof2","title":"GoF 2"}}},{"node":{"fields":{"slug":"/old/02-theory/11_solid","title":"SOLID"}}},{"node":{"fields":{"slug":"/old/02-theory/08_grasp3","title":"GRASP 3"}}},{"node":{"fields":{"slug":"/old/02-theory/design_class_diagram","title":"UML Design Class Notation"}}},{"node":{"fields":{"slug":"/old/02-theory/design_class_relations","title":"UML Design Class Relations"}}},{"node":{"fields":{"slug":"/old/02-theory/design_object_diagram","title":"UML Object Diagram Notation"}}},{"node":{"fields":{"slug":"/old/02-theory/99-exercises","title":"Exercises"}}},{"node":{"fields":{"slug":"/old/02-theory/design_package_diagram","title":"UML Package Notation"}}},{"node":{"fields":{"slug":"/old/02-theory/domain_class_diagram","title":"UML Domain Model"}}},{"node":{"fields":{"slug":"/old/02-theory/reading-instructions","title":"Reading Instructions"}}},{"node":{"fields":{"slug":"/old/03-assignments/01-assignment_1","title":"Assignment 1"}}},{"node":{"fields":{"slug":"/old/03-assignments/02-assignment_2","title":"Assignment 2"}}},{"node":{"fields":{"slug":"/old/03-assignments/03-assignment_3","title":"Assignment 3"}}},{"node":{"fields":{"slug":"/old/03-assignments/04-yahtzee","title":"Yahtzee Assignment"}}},{"node":{"fields":{"slug":"/old/old_template/ovningsuppgifter-del-a","title":"Övningsuppgifter del A"}}},{"node":{"fields":{"slug":"/old/old_template/ovningsuppgifter-del-b","title":"Övningsuppgifter del B"}}},{"node":{"fields":{"slug":"/old/03-assignments/01-assignment_1/lending-system-problem-description","title":"Stuff Lending System Problem Description"}}},{"node":{"fields":{"slug":"/old/03-assignments/01-assignment_1/yacht-club-problem-description","title":"Yacht Club Problem Description"}}},{"node":{"fields":{"slug":"/old/old_template/videologgar","title":"Vlog"}}},{"node":{"fields":{"slug":"/old/old_template/codeblock/1-index","title":"Sub Page"}}},{"node":{"fields":{"slug":"/old/03-assignments/02-assignment_2/test_specification","title":"Test Specification"}}},{"node":{"fields":{"slug":"/old/old_template/codeblock","title":"Syntax Highlighting"}}},{"node":{"fields":{"slug":"/old/old_template/ovningsuppgifter-del-a/01-hello-world","title":"Hello world"}}},{"node":{"fields":{"slug":"/old/old_template/ovningsuppgifter-del-a/02-simple-addition","title":"Enkel addition"}}}]}},"pageContext":{"id":"a0e8605b-7c48-52f5-9ebe-8fef7a1e5571"}},
    "staticQueryHashes": ["253607798","3706406642","710574383"]}