title: Why is constructor of super class invoked when we declare the object of sub class? (Java)
body: <p>Consider this code:</p>
  
  <pre><code>class Test {
      Test() {
          System.out.println("In constructor of Superclass");
      }
  
      int adds(int n1, int n2) {
          return(n1+n2);
      }
  
      void print(int sum) {
          System.out.println("the sums are " + sum);
      }
  }
  
  
  class Test1 extends Test {
      Test1(int n1, int n2) {
          System.out.println("In constructor of Subclass");
          int sum = this.adds(n1,n2);
          this.print(sum);
      }
  
      public static void main(String[] args) {
          Test1 a=new Test1(13,12);
          Test c=new Test1(15,14);
      }
  }
  </code></pre>
  
  <p>If we have a constructor in super class, it will be invoked by every object that we construct for the child class (ex. Object <code>a</code> for class <code>Test1</code> calls <code>Test1(int n1, int n2)</code> and as well as its parent <code>Test()</code>). </p>
  
  <p>Why does this happen?</p>
  
  <p>The output of this program is:</p>
  
  <blockquote>
    <p>In constructor of Superclass</p>
    
    <p>In constructor of Subclass</p>
    
    <p>the sums are 25</p>
    
    <p>In constructor of Superclass</p>
    
    <p>In constructor of Subclass</p>
    
    <p>the sums are 29</p>
  </blockquote>