Saturday, July 30, 2011

What are Static Constructors and how Static Constructors are invoked?

Static Constructors: A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
Example :-


OUTPUT of above code :


NOTE : Static Constructors doesn't take any Access Modifier. This is rule. By default is private.

    class A
    {
        /// <summary>
        /// Static Constructors doesn't take any Access Modifier. This is rule.
        /// </summary>
        static A()
        {
            Console.WriteLine("A : Static Constructor.");
        }

       /// <summary>
        /// Default Constructor
        /// </summary>
        public A()
        {
            Console.WriteLine("A : Default Constructor.");
        }
    }

When we have static constructors in our class then it would be called first.
There can be several scenarios with static constructors.

1.       If we have static and default constructors in same class then first Static constructor would be called then Default.
2.       If we have three classes A,B,C have static constructors and are inherited in such a way C:B:A, then constructors would be invoked in following sequence :-
a.       Default Constructors would be called in A:B:C sequence. Rule is, first base class default constructor will call then derived class default constructor.
b.      Static Constructors would be called in C:B:A sequence. (Opposite to invocation of Default Constructors.). Rule is, first derived class static constructor will be called then base class static constructor.

Here is the example of the same.
Making object of class C all constructors would get invoked. First all static constructors then default constructors.


Hence we can say that :-
1.       For static constructor : First derived class static constructor will be called then base class static constructor.
2.       For default constructor : First base class default constructor will call then derived class default constructor.

Another example: 




Friday, July 29, 2011

What are association, aggregation and composition in OOPs?

Association

Association is a relationship where all object have their own life cycle 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 life cycle. Both can create and delete independently.
Points:
  • Is a Relationship between objects.
  • Objects have independent life cycles.
  • There is no owner.
  • Objects can create and delete independently.

Aggregation

Aggregation is a specialize form of Association where all object have their own life cycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Points:
  • Specialize form of Association.
  • has-a relationship between objects
  • Object have independent life-cycles
  • Parent-Child relationship

On the other hand aggregation relation just represents reference between classes. For example consider Employee and contact information. Blow diagram represents the UML



The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like "Employee has a Address". Let us see how it can be implemented in C#.

public class Address
{
 . . .
}

public class Employee

{

     private Address address;

     public Employee(Address address)

     {

         this.address = address;

     }

     . . .

}

Composition

Composition is again specialize form of Aggregation. It is a strong type of Aggregation. Child object does not have their life cycle and if parent object deletes all child object 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 cannot belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will automatically delete.
Points:
  • Specialize form of Aggregation.
  • Strong Type of Aggregation.
  • Parent-Child relationship.
  • Only parent object has independent life-cycle.

Lets take a simple example of Cycle. Cycle should contain 2 tyres and if we convert it to logical model below is the UML diagram




The above UML describes that Cycle contains 2 Tyres and the life time of each Tyre is maintained by Cycle. It is like "Tyre is Part Of Cycle". Let us see how it can be implemented in C#.

public class Tyre
{
 . . .
}

public class Cycle

{

    Tyre[] tires = new Tyre[]{new Tyre(), new Tyre()};

    .......

}

Have a grate day.