views:

7081

answers:

5

Hello,

I have a question about default constructors and inheritance in Java.

Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don't write any default constructor, then Java does not provide a default constructor. My question is: what is the case with classes, which inherit from other classes - if I write a constructor with some parameters in them, but don't include a default constructor, do they inherit the default constructor of the super class?

Thank you.

+12  A: 

Constructors are not inherited.

Also, the initialization of fields is done by the virtual machine, not the default constructor. The default constructor just invokes the default constructor of the superclass, and the default constructor of Object is empty. The good point of this design is that there is no way to ever access uninitialized fields.

starblue
Yes, I've just understood this, potyl below has pointed this out. But how (and when) are the fields initialised then? (please, see my comment on the potyl's answer).
Is it the case that when I create the object, then Java automatically gives the default values? But then again, we do create objects with constructors, right?
Yes, it took a while to do the research w.r.t. initialization.
starblue
Thank you, I have also found a nice article exactly about this - initialization in Java, here: http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=1
+3  A: 

Unless you use super(...) a constructor calls the empty constructor of its parent. Note: It does this on all you classes, even the ones which extend Object.

This is not inheriting, the subclasses don't get the same constructors with the same arguments. However, you can add constructors which call one of the constructors of the super class.

Peter Lawrey
With "empty constructor" you mean the default constructor, right?
And what if the parent does not have a default constructor?
Here I mean a constructor with no arguments. Typically the default constructor is one which is not defined at all. You can define a constructor with no arguments.
Peter Lawrey
If the parent does not have a constructor with no arguments, you must call super(...) for a valid constructor in the super class.
Peter Lawrey
You mean for a "valid constructor in the " SUB (derived) class ?
Btw, I think we call a constructor "default", if it does not have any arguments (parameters) - it doesn't matter whether you define it, or Java. (?)
It seems that I am wrong - a constructor without parameters (or arguments) is called "a non-argument constructor", and the "default" constructor is the one, which is provided automatically by Java, if you don't write any constructor in the class. It is a non-argument constructor.I have read this:
here: http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=1
+2  A: 

If you provide a constructor then Java will not generate you a default empty constructor. So your derived class will only be able to call your constructor.

The default constructor doesn't initialize your private variables to default values. The proof is that it's possible to write a class that doesn't have a default constructor and has its private members initialized to default values. Here's an example:

public class Test {

 public String s;
 public int i;

 public Test(String s, int i) {
  this.s = s;
  this.i = i;
 }

 public Test(boolean b) {
  // Empty on purpose!
 }

 public String toString() {
  return "Test (s = " + this.s + ", i = " +  this.i + ")";
 }

 public static void main (String [] args) {
  Test test_empty = new Test(true);
  Test test_full = new Test("string", 42);
  System.out.println("Test empty:" + test_empty);
  System.out.println("Test full:"  + test_full);
 }
}
potyl
That's interesting! I have lived quite a long time with the assumption that if you don't initialize the instance variables in a constructor, then Java does this in the default constructor. So, in this case how are the variables initialized?
Here I have found a nice article about initialization in Java, answering my question: http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=1
Creating an object instance requires that the operator "new" allocates memory and invokes a constructor (used for initial logic). Our constructors take care only of the latter. I'm guessing that the first part takes care of the default initialization of the data members while allocating memory.
potyl
+2  A: 
  1. If you do not make a constructor, the default empty constructor is automatically created.

  2. If any constructor does not explicitly call a super or this constructor as its first statement, a call to super() is automatically added.

Always.

paulmurray
A: 

class Box{ double width; double height; double depth; Box(Box ob){ width=ob.width; height=ob.height; depth=ob.depth; }

Box (double w,double h,double d){
    width=w;
    height=h;
    depth=d;
     }
Box(double len){
    width=height=depth=len;
}
*****Box(){

}*****
double volume(){
    return width*height*depth;
}

} class Boxweight extends Box{ double weight; Boxweight(double w,double h,double d,double m){ width=w; height=h; depth=d; weight=m;

}

} class Access { public static void main(String args[]){ Boxweight mybox1=new Boxweight(10,20,30,3); Boxweight mybox2=new Boxweight(1,2,3,30); double vol; vol=mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println("");

    vol=mybox2.volume();
    System.out.println("Volume of mybox2 is " + vol);
    System.out.println("weight of mybox1 is " + mybox2.weight);


}

}

In the above prog ram without the empty constructor box the program cannot run,can anyone tell me the reason????????