Monday, May 7, 2012

Object Relations

Here is an attempt to make better sense out of the in class lecture on objects and types.

Classes that talk to each other are said to have an Association.

Association just means that one class calls methods in the other one.

Composition represents a whole/part relationship. Further it represents a relationship in which the part is contained in the whole. If the whole is destroyed, so is the part. This is represented by a line with a filled in diamond at the end. The diamond is attached to the class which represents the whole. In the example below, Scan is a part of Scanner. It is created by the scanner and when the scanner is destroyed the Scan is also.

Inheritance represents a generalization, specialization relationship. The parent represents the more general case--in the example below, person. The children represent more specific cases: Employee, Visitor. Inheritance is important for code reuse. A child class inherits all the public properties and methods of the parent. That means they can add just those properties and methods that are specific to them. It is also possible to override parent methods, if necessary, and give them different behaviors.

In most modern languages a child can have only one parent. But it can inherit from as many interfaces as desired. An interface consists only of method signatures. Interfaces are sometimes referred to as contracts. A class that implements an interface must implement any of the methods in the interface. It is a way of guaranteeing that a given class will have the appropriate methods to talk to existing objects in the ways they expect. An interface is represented like inheritance. In the example below an Observer object implements an interface called IUpdate that contains the signature for an Update method.

The final type of object I will show here is an enumeration. An enumeration just assigns a friendly name to an numeric value to make it easer to refer to in code.

I will upload code examples of each of these relations in the near future

No comments:

Post a Comment