Category: Object Oriented Programming

Understanding association, aggregation and composition

Association is a relationship where all objects have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. 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 create and delete independently.

Aggregation is a specialised form of Association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not be destroyed. We can think about it as a “has-a” relationship.

Composition is again specialised form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have its lifecycle and if parent object is deleted, all child objects will also be deleted. Let’s take again an example of relationship between House and Rooms. House can contain multiple rooms – there is no independent life of room and any room can not belong to two different houses. If we delete the house – room will automatically be deleted. Let’s take another example relationship between Questions and Options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically be deleted.

Hope this will help !!!

Jay Ganesh

Reference: http://www.dotnet-tricks.com/Tutorial/oops/T0Lb270813-Understanding-Association,-Aggregation,-Composition-and-Dependency-relationship.html

OOPS – Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)

This Article summarizes the following points written by Akhil Mittal :

  • In C#, We can equate an object of a base class to a derived class but not vice versa.
  • The override modifier is needed as the derived class methods will get first priority and be called upon.
  • These modifiers like new and override can only be used if the method in the base class is a virtual method. Virtual means that the base class is granting us permission to invoke the method from the derived class and not the base class. But, we have to add the modifier override if our derived class method has to be called.
  • If the base class object declared the method virtual and the derived class used the modifier override, the derived class method will get called. Otherwise, the base class method will get executed. Therefore for virtual methods, the data type created is decided at run time only.
  • All the methods not marked with virtual are non virtual, and the method to be called is decided at compile time, depending upon the static data type of the object.
  • An override method is a method that has the override modifier included on it. This introduces a new implementation of a method. We can’t use the modifiers such as new, static or virtual along with override. But abstract is permitted.

Read full article – http://www.codeproject.com/Articles/774578/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Dyn

Hope this will help !!!

Jay Ganesh