views:

78

answers:

3

In the following code:

import java.io.*;

public class MyClass1
{
     MyClass1()
     {
         System.out.println("base class");
     }
     public void print()
     {
        System.out.println("base print");
     }
}

class ChildClass extends MyClass1
{
    public ChildClass()
    {
       System.out.println("child class");
    }
    public void print()
    {

      System.out.println("child print");
    }
}

Why is it that when I create an instance of type ChildClass the constructor of the base class is also executed??

+2  A: 

Because that's what's supposed to happen :-)

Your derived class uses the base class as a foundation. In OO speak it is-a base class. That base class also needs to initialise itself, and consequently its constructor must be called.

It's not obvious from your example, but it will make more sense if you give your base class some (protected) members. Initialise them in the base constructor, and consequently they will have the expected values when viewed from your derived class upon construction.

See below. The field value is visible in the child class. What would you expect as the initialised value ?

public class MyClass1
{
     protected int value;
     MyClass1()
     {
         System.out.println("base class");
         this.value = 42;
     }
}

class ChildClass extends MyClass1
{
    public ChildClass()
    {
       // what would you expect 'value' to be here ?
       System.out.println("child class " + value);
    }
}
Brian Agnew
+5  A: 

Because your child class extends the base class - it's an instance of the base class and has all of the same fields and variables, etc. Thus the base class must also be instantiated.

For a concrete example, imagine your base class had the following in:

public class Base
{
    final private int id;

    public Base()
    {
        this(-1);
    } 

    public Base(int id)
    {
        this.id = id;
    }

    public getId()
    {
        return id;
    }
}

A final variable is guaranteed to be instantiated when the class is constructed. Your child class will have an id field (even if it cannot access it directly with child methods), and since this field is private you cannot possible instantiate it with the child constructor - so a base class constructor must be called.

Bear in mind that this isn't solely an issue with final variables, nor is it unique to any particular features you may use - since your child class is a base class, it needs to be properly instantiated as one.

Andrzej Doyle
A: 

Because compiler by default add super() constructor in the child class constructor if it is not specified . Every Constructor Should have either this() in case of without inheritance or super() method when ever there is an inheritance . To illustrate it i have taken this example .

  public class Vehicle {
    protected int wheels;
    protected int lights;

    Vehicle(){
     System.out.println("Vehicle Class Constructor");
     this.wheels=4;
     this.lights=2;
    }
 }

Vehicle is the parent class

 class Car extends Vehicle {
  public Car(){
    #Compiler add the super() constructor by default
     System.out.println("Car class constructor");
    }
}

Car is the Child class

public class TestCar {
   public static void main(String args[]){
       Car c = new Car();
       System.out.println("Wheels" + c.wheels);
       System.out.println("Lights" + c.lights);
   }
}

In above code snippet When i compile the TestCar.java file during the Compile time the compiler looks for the Car constructor and checks whether Car class has any parent as soon as it checks that Car class extends the parent class Vehicle , it checks whether user had provided super() in inheritance tree . if not it adds one .

Hope this helps !

YetAnotherCoder