CLASS DIAGRAMS A classy approach to objects
The Basic Class Diagram Class Name Attributes (- indicates private each would have properties or accessor/mutator methods) Class Methods Customer ClassElements
Class Relationships Classes have relationships with other classes These fall into 4 different kinds Associations Inheritance (generalization/specialization) Aggregation (parts to whole, where parts have a separate existence) Composition (parts to a whole where the parts do not have a separate existence
Associations Associations means that a class interacts with another class A customer for instance interacts with a purchase These associations can also contain a property called “multiplicity” Multiplicity specifies how many instances of each class an association a class can have For instance one customer can have many (1..* ) purchases, or one class can have 25 students (1..25)
Association Example
Inheritance In Inheritance child classes inherit from a parent class This means they get all the public properties and methods of the parent without having to rewrite them Usually the parent is a generalized class that contains all the elements the child classes have in common, and the child classes represent more specialized instances of that general class
Inheritance Example
Aggregation Aggregation exists when classes have a whole, part relationship but the parts have an independent existence of their own For instance a company exists of many parts, but each of those parts can be treated as an object in themselves
Aggregation Example
Composition Composition describes a relationship where the parts cannot have a separate existence from the whole. An example is the.Net DataTable class. It contains the classes DataRow and DataColumn, but they can only exist in the DataTable class
Composition Example
Interfaces Interfaces contain only method definitions. They are often referred to as “contracts” When a class implements an interface it “contracts” to provide an implementation for all the methods in the interface Interfaces are useful for ensuring the consistency of some kinds of common functionality. A developer knows that if a class implements a particular interface then it will contain definitions for the interfaces methods
Interface Example Both classes must implement the three methods, as well as their own methods