Object-Oriented Hierarchies
Types of Hierarchies
- Has-a / Part-of (Association/Containment)
- Represents component relationships
- Example: Car has-a Engine
- Is-a / Kind-of (Inheritance/Generalization)
- Represents type relationships
- Example: Employee is-a Person
- Use-a (Dependency)
- When one class uses another class but doesn't own it
- Example: Class A uses Class B as a method parameter
- Creates-a (Instantiation)
- When one class creates instances of another class
- Common in factory design patterns
Association (Has-a Relationship)
- When an object is part of or component of another object
- Implemented by declaring an object of one class as a data member in another class
class Engine {};
class Car {
private:
Engine e; // Association
};
Types of Association
- Composition (Strong Association)
- Dependency object cannot exist without the dependent object
- Represents tight coupling
- Example: Heart cannot exist without the Human
- Aggregation (Weak Association)
- Dependency object can exist without the dependent object
- Represents loose coupling
- Example: Department and Faculty
Inheritance (Is-a Relationship)
- When one class is a specialized version of another class
- Syntax:
class Derived : [mode] Base {}
- Base class is an abstraction for the derived class
What Gets Inherited
- All data members (private/protected/public, static/non-static)
- Only non-static data members get space in the object