OOPortal OOPortal


User Defined Types  «Prev  Next»
Lesson 7 Class composition
Objective Describe how Class Attributes are not limited to a Language's built-in Data Types

Describe how Class Attributes are not limited to a Language's built-in Data Types in OO Analysis

Class attributes are not limited to a language's built-in data types. Classes may contain attributes that are themselves classes.
This slide show illustrates this concept:

1) As you may remember from high school geometry, a line is defined by two points. Thus the Line class might have two Point attributes.
2) Composition is when one class contains another class as an attribute. Composition is indicated in a class diagram by connecting the owning class to the owned class with a line.
3) The type and direction of the relationship is indicated by a few words such as (has a), (owns a) , or (refers to) followed by an arrowhead indicating the direction of the relationship
4) This picture only shows a single point, when in fact a Line object contains two Point objects. This is indicated by adding the numeral 2 above the line and next to the point.


Line Point Composition
Using attributes that are instances of other classes makes sense for you system. If a department has a manager, then it makes sense for the Department class to have an attribute that is an instance of the Manager class that is a Manager object.

Object is an Instance of a Class

Each object is an instance of a class, which can be seen as a blueprint or template of the object's characteristics. Contrary to procedural programming, these characteristics include data, (attributes or variables describing the object's state), and behaviors (methods or procedures describing the actions an object can perform).
A simple example can help explain this. Imagine you are developing an application to keep track of courses and student registrations. In procedural programming, your first task would be to come up with an appropriate data structure to represent the concepts you are dealing with. You thus might define two lists
  1. one for holding the students and
  2. one for holding the courses.
Each list would contain a dictionary of values representing a single student or course. This might look as follows:

STUDENTS = [
{id : 'S0001', last: 'Demmick', first: 'Larry', birthdate: '1989-05-13'},
{id : 'S0002', last: 'Newandyke', first: 'Freddy', birthdate: '1991-01-05'},
...
]
COURSES = [
{id : 'C00A', name: 'Introduction to Java'},
{id : 'C00B', name: 'Advanced Data Base Management'},
...
]
Pay no attention to the syntax being used here. It is only pseudo-code to illustrate the point. The following step defines a series of operations.procedures.that you want to perform on your concepts.
For example, you will need a procedure to add a student:
procedure add_student(i, n, fn, bd) {
STUDENTS += {id : i, name: n, firstname: fn, birthdate: bd}
}