Has-a
Encyclopedia
In database design
Database design
Database 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 of
Object composition
In 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 a member field of an object. Multiple has-a relationships will combine to form a possessive hierarchy. This is contrasted with an Is-a
Is-a
In knowledge representation, object-oriented programming and design, is-a or is_a or 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++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 STL
Standard Template Library
The Standard Template Library is a C++ software library which later evolved into the C++ Standard Library. It provides four components called algorithms, containers, functors, and iterators. More specifically, the C++ Standard Library is based on the STL published by SGI. Both include some...

.

Examples

In databases has-a relationships are usually represented in an Entity-relationship model
Entity-relationship model
In 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.

In object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 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
Carburetor
A carburetor , carburettor, or carburetter is a device that blends air and fuel for an internal combustion engine. It is sometimes shortened to carb in North America and the United Kingdom....

, or a car is "composed of" a carburetor. When the diamond is coloured black it signifies composition
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...

, 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.

Here comes a bad example in which your assertion is required. Another way to distinguish between composition
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...

 and aggregation in modeling the real world, is to consider the relative lifetime of the contained object. For example, if a Car object contains a Chassis object, a Chassis will most likely not be replaced during the lifetime of the Car. It will have the same lifetime as the car itself; so the relationship is one of composition
Object composition
In computer science, object composition is a way to combine simple objects or data types into more complex ones...

. On the other hand, if the Car object contains a set of Tire objects, these Tire objects may wear out and get replaced several times. Or if the Car becomes unusable, some Tires may be salvaged and assigned to another Car. At any rate, the Tire objects have different lifetimes than the Car object; therefore the relationship is one of aggregation.

The diagram on the right shows a very common misuse of the aggregation concept. It is nowadays common to see almost every relationship in a UML model marked as an aggregation. However it makes no sense to say that "a pond is an aggregation of ducks". The diagram is also incorrect in its example of composition. A carburetor is not necessarily dependent on the car for existence. It could be on a shelf somewhere. The car is an aggregate of its parts. An order-line on the other hand lives and dies with the order. It is therefore correct to say that the order is a composition of order-lines.

If one were to make a C++ software Class to implement the relationships(Composition/Aggregation) described above, the Car object would contain a complete Chassis object in a data member. This Chassis object would be instantiated in the constructor of the Car class (or defined as the data type of the data member and its properties assigned in the constructor.) And since it would be a wholly contained data member of the Car class, the Chassis object would no longer exist if a Car class object was to be deleted(in other words, the relationship between a car and its chassis is identified to be NOT ONLY of aggregation, but more precisely of Composition). Composition makes a relatively stricter demand on the lifetime of container and contained objects in it.

On the other hand, the Car class data members that point to Tire objects would most likely be C++ pointers. Tire objects could be instantiated and deleted externally, or even assigned to data members of a different Car object. Tire objects would have an independent lifetime e.g. to be assigned to another suitable vehicle and separate from when the Car object was deleted, which makes the relationship between a car and its resuable tires of Aggregation, as the lifetime of tires doesn't necessarily depend on the lifetime of the car.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK