title: Constructor in Objective-C
body: <p>I was trying this sample program below.</p>
  
  <p>I'm not calling the +(void)initialise and -(id)init method in the class B.But its getting called automatically.</p>
  
  <p>Is the -(void)initialise is equal to the default constructor in objective C.</p>
  
  <p>Does the [super init] points to the NSObject.</p>
  
  <p>If i'm not using the -(id)init method i'm getting a warning that the class is with incomplete implementation. </p>
  
  <p>ClassA.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  static int ab;
  
  @interface ClassA : NSObject {
      int a;
  }
  
  + (void) initialize;
  - (id) init;
  - (void) displayNumOfInstance;
  - (void) disp;
  
  @end
  </code></pre>
  
  <p>ClassA.m</p>
  
  <pre><code>#import "ClassA.h"
  
  @implementation ClassA
  
  + (void) initialize
  {
      ab=0;
  }
  
  - (id) init
  {
      self = [super init];
      if (self!=nil) {
          ab++;
      }
      return self;
  }
  
  - (void) displayNumOfInstance
  {
      NSLog(@"Number of instances of this class:%d",ab);
  }
  
  - (void) disp
  {
      NSLog(@"The value is %d",ab);
  }
  
  @end
  </code></pre>
  
  <p>ClassB.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "ClassA.h"
  
  @interface ClassB : ClassA {
  
  }
  
  - (void) display;
  
  @end
  </code></pre>
  
  <p>ClassB.m</p>
  
  <pre><code>#import "ClassB.h"
  
  @implementation ClassB
  
  - (void) display
  {
      ab=20;
      NSLog(@"The value ab is %d",ab);
  }
  
  @end
  </code></pre>
  
  <p>class2.m</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "ClassA.h"
  
  int main (int argc, const char * argv[]) {
      ClassA *a = [[ClassA alloc]init];
      [a disp];
      [a release];
  
      ClassB *b = [[ClassB alloc]init];
      [b display];
      [b release];
  
      ClassA *a1 = [[ClassA alloc]init];
      [a1 disp];
      [a1 release];
  
      ClassB *b1 = [[ClassB alloc]init];
      [b1 display];
      [b1 release];
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>2011-04-30 15:31:42.490 class2[1674:a0f] 1
  2011-04-30 15:31:42.493 class2[1674:a0f] The value ab is 20
  2011-04-30 15:31:42.494 class2[1674:a0f] 2
  2011-04-30 15:31:42.495 class2[1674:a0f] The value ab is 20
  </code></pre>