In my previous post (OOPS - A detailed understanding of concepts), I had tried to provide a detailed understanding of basic concepts of OOPS (Class, Object, Abstraction, Encapsulation, Polymorphism and Inheritance). In this blog I am providing an overview of relationships between the classes.
Generalization and Specialization both refer to inheritance, but the way in which it is implemented or looked into makes the difference. If many similar existing objects are combined to form a superclass to do the job of its subclass', then it is known as Generalization. When viewed from the perspective of a base class, it is generalization because the base class has functions or operations that may be commonly used by all of its subclasses.
If some new subclasses are created from an existing superclass to do specific job of the superclass, then it is known as specialization. When viewed from the perspective of the derived class, it is specialization because even though the common functionalities from the super class are available, the derived classes perform their own operations or functions specific to the purpose of their creation.
Both concepts can be implemented using inheritance only. But the order of creation of the subclass and the superclass drives the concept name.
Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior (i.e., previously coded algorithms associated with a class) from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance give rise to a hierarchy.
Consider a class Person that contains a person's name, address, phone number, age, and gender. We can define a subclass of Person called Student that contains the person's grade point average and classes taken, and another subclass of Person called Employee that contains the person's job-title, employer, and salary.
Singleness: Using single inheritance, a subclass can inherit from only one superclass. Person can be either a Student or an Employee, but not both. Using multiple inheritance partially solves this problem.
Static: It does not allow a Student object to become an Employee object while retaining the state of its Person superclass. Although similar behavior can be achieved with the decorator pattern.
Association is a relationship where all object have their own lifecycle and there is no owner e.g., Teacher and Student classes. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
- Relationship between objects (One to Many, Many to Many).
- Objects have independent lifecycles.
- There is no owner.
- Objects can be created and deleted independently.
Association is the more general term that define the relationship between two classes, where as the aggregation and composition are relatively special.
Aggregation is a specialized form of Association where all object have their own lifecycle but there is an ownership and child object cannot belong to another parent object e.g., Department and Teacher (Parent and Child). A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not destroy. It can be thought of as “has-a” relationship.
- “Has-a” relationship between objects
- Object have independent life-cycles
- Parent-Child relationship, Parent owns child
Composition is again a specialized form of Aggregation. It is a strong type of Aggregation. Child object does not have their independent lifecycle and if parent object is deleted all child object will also be deleted, e.g., relationship between House and Rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different houses and if we delete the house room will automatically be deleted.
- Strong Type of Aggregation.
- Parent-Child relationship, Parent owns child.
- Only parent object has independent life-cycle.
Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, this can be described as the relationship between the interface and the implementing class, e.g., a particular model of a car ‘Ford-Ikon’ that implements the blueprint of a car realizes the abstraction.
If change in structure or behavior of one class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. This usually happens when one class contains the other class, e.g., Relationship between shape and circle is dependency.