In
database designDatabase design is the process of producing a detailed data model of a database. This logical data model contains all the needed logical and physical design choices and physical storage parameters needed to generate a design in a Data Definition Language, which can then be used to create a database...
and object oriented program architecture,
has-a is a relationship where one object (often called the composited object) "belongs" to (is
a part or member ofIn computer science, object composition is a way to combine simple objects or data types into more complex ones...
) another object (called the composite type), and behaves according to the rules of ownership. In simple words,
has-a relationship in an object is called member field of an object. Multiple
has-a relationships will combine to form a possessive hierarchy.
In
database designDatabase design is the process of producing a detailed data model of a database. This logical data model contains all the needed logical and physical design choices and physical storage parameters needed to generate a design in a Data Definition Language, which can then be used to create a database...
and object oriented program architecture,
has-a is a relationship where one object (often called the composited object) "belongs" to (is
a part or member ofIn computer science, object composition is a way to combine simple objects or data types into more complex ones...
) another object (called the composite type), and behaves according to the rules of ownership. In simple words,
has-a relationship in an object is called member field of an object. Multiple
has-a relationships will combine to form a possessive hierarchy. This is contrasted with an
Is-aIn knowledge representation and object-oriented programming and design, is-a is a relationship where one class D is a subclass of another class B ....
relationship which constitutes a different kind of hierarchy (subtyping). The decision whether the most logical relationship for an object and its subordinate is not always clearly
has-a or
is-a. Confusion over such decisions have necessitated the creation of these metalinguistic terms. A good example of the
has-a relationship is containers in the
C++C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features...
STLThe Standard Template Library is a software library partially included in the C++ Standard Library. It provides containers, iterators, algorithms, and functors. More specifically, the C++ Standard Library is based on the STL published by SGI. Both include some features not found in the other...
.
Examples
In databases has-a relationships are usually represented in an
Entity-relationship modelIn software engineering, an Entity-Relationship Model is an abstract and conceptual representation of data. Entity-relationship modeling is a database modeling method, used to produce a type of conceptual schema or semantic data model of a system, often a relational database, and its requirements...
. As you can see by the diagram on the right an account can have multiple characters. This shows that account has a "has-a" relationship with character.
In
object-oriented programmingObject-oriented programming is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as information hiding, data...
this relationship can be represented with a Unified Modeling Language diagram. This has-a relationship is also known as composition. As you can see from the diagram on the right a car "has-a " carburetor, or a car is "composed of" a carburetor. When the diamond is coloured black it signifies composition, i.e. the object on the side closest to the diamond is made up of or contains the other object. While the white diamond signifies aggregation, which means that the object closest to the diamond can have or possess the other object.