11
Association / Aggregation / Composition and inheritance

Association / Aggregation / Composition and inheritance

Embed Size (px)

Citation preview

Page 1: Association / Aggregation / Composition and inheritance

Association / Aggregation / Composition and inheritance

Page 2: Association / Aggregation / Composition and inheritance

Association

Relationship where all object have their own lifecycle and there is no owner.

Ex: Airplane and passengers. No ownership between the objects and both have their own lifecycle. Both can create and delete independently

Page 3: Association / Aggregation / Composition and inheritance

AggregationSpecial form of Association where all object have their own lifecycle but there is ownership ( Has a relationship) and child object can not belongs to another parent object.

Ex: Department and teacher.

A single teacher can not belongs to multiple departments, but

if we delete the department teacher object will not destroy.

Page 4: Association / Aggregation / Composition and inheritance

Composition

Special form of Aggregation . Child object dose not have their lifecycle and if parent object deletes, all child object will also be deleted (ownership). Ex : Person and hands.

Page 5: Association / Aggregation / Composition and inheritance
Page 6: Association / Aggregation / Composition and inheritance

Inheritance : ”is” relationship

An employee is a person

Page 7: Association / Aggregation / Composition and inheritance

• Association No ownershipOwn lifecycle

• AggregationOwnership ( has a..)Own lifecycle

• CompositionOwnership (composed of ..) No Own lifecycle

• InheritanceIs relationship

Page 8: Association / Aggregation / Composition and inheritance

Association

8

Student * 5..60 Take Teach 0..3 1

Teacher Faculty Course

public class Student {

/** Data fields */

private Course[]

courseList;

/** Constructors */

/** Methods */

}

public class Course {

/** Data fields */

private Student[]

classList;

private Faculty faculty;

/** Constructors */

/** Methods */

}

public class Faculty {

/** Data fields */

private Course[]

courseList;

/** Constructors */

/** Methods */

}

An association is usually represented as a data field in the class.

Page 9: Association / Aggregation / Composition and inheritance

Representing Aggregation/composition in Classes

An aggregation / composition relationship is usually represented as a data field in the aggregated class.

9

public class Name {

/** Data fields */

/** Constructors */

/** Methods */

}

public class Person {

/** Data fields */ private Name name;

private Address address;

/** Constructors */

/** Methods */

}

public class Address {

/** Data fields */

/** Constructors */

/** Methods */

}

Name Address Person

Composition Aggregation

Page 10: Association / Aggregation / Composition and inheritance

Inheritance

Inheritance models the is-an-extension-of relationship between two classes.

10

Person Faculty

public class Faculty extends Person {

/** Data fields */

/** Constructors */

/** Methods */

}

(A) (B)

Page 11: Association / Aggregation / Composition and inheritance

Borrowing Loans

11

Name BorrowerPersonLoan Address

Loan

Borrower -loan: Loan +Borrower() +Borrower(name: Name, address: Address) +getLoan(): Loan +setLoan(loan: Loan): void +toString(): String

Address -street: String -city: String -state: String -zip: String +Address() +Address(street: String, city: String, state: String, zip: String) +getStreet(): String +getCity(): String +getState(): String +getZip(): String +setStreet(street: String): void +setCity(city: String): void +setState(state: String): void +setZip(zip: String): void +getFullAddress(): String

Defined in Example 6.7

Person -name: Name -address: Address +Person() +Person(name: Name, address: Address) +getName(): Name +seName(name: Name): void +getAddress(): Address +setAddress(address: Address): void +toString(): String

Name -firstName: String -mi: char -lastName: String +Name() +Name(firstName: String, mi: char, lastName: String) +getFirstName(): String +getMi(): char +getLastName(): String +setFirstName(firstName: String): void +setMi(mi: char): void +setLastName(lastName: String): void +getFullName(): String