HAS-A-Relationship in Apex
What is HAS-A-Relationship: It is basically used for code reusability. It means that an instance of a class has a reference to another class or the other instance.
Why use HAS-A-Relationship?
- For Code Reusability.
Note: Using HAS-A-Relationship, we cannot achieve runtime polymorphism. Only via Inheritance we can achieve runtime polymorphism.
Aggregation: If a class has an entity reference, it is called as Aggregation. Aggregation is a type of HAS-A relationship.
Take an example, Room object contains many chairs. If we take away chairs away from room there is no impact on the existence of Room, as given below.
public class Room { String roomName; Chair ch; //Chair is a class. }
Composition: If a class has an entity reference, it is known as Composition. Composition is also a type of HAS-A relationship.
Take an example, Room object contains many walls. If we take away walls away from room there will be no existence of Room, as given below.
public class Room { String roomName; Wall wal; //Wall is a class. }