0% found this document useful (0 votes)
51 views8 pages

Generalization Aggregation

Concept Overriding and generalization of classes

Uploaded by

k13913703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Generalization Aggregation

Concept Overriding and generalization of classes

Uploaded by

k13913703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

**Generalization**
Generalization is the process of extracting shared characteristics from two or more classes and
combining them into a generalized superclass. The derived classes inherit properties and behaviors from
the superclass.

** Example: **

- **Superclass: ** `Animal`

- **Subclasses: ** `Dog`, `Cat`, `Bird`

class Animal {

public:

void eat () {/*...*/}

};

class Dog: public Animal {/*...*/};

class Cat: public Animal {/*...*/};

**Diagram: **

Animal

/ \

Dog Cat

2. **Sub-typing (Extension)**
Sub-typing refers to creating new types (subclasses) by extending an existing type (superclass). This is
an "is-a" relationship where the subclass inherits properties from the superclass.

**Example: **

- `Dog` is a subtype of `Animal`, and `Cat` is another subtype of `Animal`.

3. **Specialization (Restriction)**
Specialization is the opposite of generalization. It refers to creating a more specific class from a general
class, restricting some features or behaviors.

**Example: **

- If `Animal` is generalized, `Dog` and `Cat` are specialized versions with additional properties, such as
`bark () ` for `Dog` and `meow () ` for `Cat`.

4. **Overriding**
Overriding occurs when a subclass provides a specific implementation of a method that is already
defined in its superclass. It improves the behavior(functionality) of the existing class.

**Example: **

class Animal {

public:

virtual void sound() {

cout << "Animal sound";

};

class Dog : public Animal {

public:

void sound() override {

cout << "Woof!";

};

- The method `sound()` is overridden in the `Dog` class to provide a specific behavior.

### 5. **Reasons for Overriding**

- **Customization:** To provide a specific behavior in a subclass.

- **Polymorphism:** To enable dynamic method invocation at runtime using base class pointers.

- **Reusability:** To reuse superclass methods while extending or modifying their behavior.

### 6. **Abstract Classes**

An abstract class is a class that cannot be instantiated directly and often contains at least one pure
virtual function.

**Example:**

```cpp

class Animal {
public:

virtual void sound() = 0; // pure virtual function

};

```

### 7. **Concrete Classes**

Concrete classes are classes that can be instantiated and provide implementations for all their
methods.

**Example:**

```cpp

class Dog : public Animal {

public:

void sound() override {

cout << "Woof!";

};

```

### 8. **Diamond Problem**

The **Diamond Problem** arises in languages like C++ with multiple inheritance, where a subclass
inherits from two classes that both inherit from a common base class.

**Example:**

```

/\

B C

\/
D

```

If both `B` and `C` inherit from `A`, and `D` inherits from both `B` and `C`, there may be ambiguity
about which version of `A`'s attributes or methods `D` should inherit.

### 9. **Solution to the Diamond Problem**

C++ resolves the Diamond Problem using **virtual inheritance**, ensuring that only one instance of
the base class is created.

**Example:**

```cpp

class A { /*...*/ };

class B : virtual public A { /*...*/ };

class C : virtual public A { /*...*/ };

class D : public B, public C { /*...*/ };

```

### 10. **Association**

Association represents a relationship between two classes where one class uses or interacts with
another class. It's a "has-a" relationship.

**Example:**

- A `Doctor` has a relationship with `Patient`.

**Diagram:**

```

Doctor ------- Patient

```

### 11. **Kinds of Association**


- **Bidirectional:** Both classes are aware of each other (e.g., `Doctor` and `Patient`).

- **Unidirectional:** One class knows about the other, but not vice versa (e.g., `Car` knows about
`Engine`, but `Engine` doesn't know about `Car`).

### 12. **Types of Simple Association**

- **One-to-One:** One object of class A is associated with one object of class B.

- **One-to-Many:** One object of class A is associated with multiple objects of class B.

- **Many-to-Many:** Many objects of class A are associated with many objects of class B.

### 13. **Ternary Association**

A ternary association involves three classes or entities.

**Example:**

- `Doctor`, `Patient`, and `Appointment`.

### **N-ary Association**

An **n-ary association** in object-oriented modeling refers to a relationship involving more than two
classes or entities. A **ternary association** (3-way relationship) is a common example, but in general,
n-ary associations can involve any number of entities (where *n* is the number of participating classes).

### **Real-Life Example 1: Hospital System (Ternary Association) **

Consider a healthcare system involving three entities:

- **Doctor**

- **Patient**

- **Appointment**

In this case, a **ternary association** (a 3-way relationship) would represent the connection between a
doctor, a patient, and a specific appointment.
- A **Doctor** schedules multiple **Appointments** with different **Patients**.

- A **Patient** can have multiple **Appointments** with different **Doctors**.

#### **Diagram (Ternary Association): **

```

Doctor ------ Appointment ------- Patient

```

#### Explanation:

In this relationship:

- Each appointment involves one doctor and one patient.

- The association **Appointment** is the entity that links both doctors and patients.

- The appointment entity may store additional information such as the appointment date, time, and
location, capturing the specific relationship between the doctor and the patient.

### **Real-Life Example 2: University Enrollment (Quaternary Association) **

In a university enrollment system, consider the following entities:

- **Student**

- **Course**

- **Semester**

- **Professor**

A **quaternary association** (a 4-way relationship) can model a situation where:

- A **Student** is enrolled in a **Course**.

- The **Course** is taught by a **Professor**.

- The course is offered in a specific **Semester**.

#### **Diagram (Quaternary Association):**


```

Student ------ Enrollment ------- Course

|--------- Professor

|--------- Semester

```

#### Explanation:

In this case:

- The **Enrollment** entity acts as the association between a student, a course, a professor, and the
semester in which the course is taken.

- The enrollment record can store specific details, such as the grade, course schedule, or other relevant
information.

### 14. **Composition**

Composition is a strong form of association where the lifetime of the contained object is bound to the
lifetime of the container. If the container is destroyed, the contained objects are also destroyed.

**Example:**

- A `House` contains `Rooms`. If the house is destroyed, the rooms are destroyed as well.

**Diagram:**

```

House <>---- Room

```

### 15. **Composition is Stronger**

Composition is stronger because it implies ownership. The container "owns" the contained objects, and
they cannot exist independently.

### 16. **Aggregation**


Aggregation is a weaker form of association where the contained objects can exist independently of
the container.

**Example:**

- A `Team` contains `Players`. If the team is disbanded, the players still exist.

**Diagram:**

```

Team ----<> Player

```

### 17. **Aggregation is Weaker**

Aggregation is weaker than composition because the lifetime of the contained objects is not
dependent on the container.

You might also like