wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on constructors" Watch "doubt on constructors" New topic
Author

doubt on constructors

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
I came across a question in the mock
It is as under :
class A {
A() {
System.out.println("Class A Constructor");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
// the output here is "Class A Constructor followed by Class B Constructor //
again similar question i came across.It is as under :

class A {
A(double d) {
System.out.println("Printing the value of d is 10");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
[a] Printing the value of d is 10 followed by Class B Constructor
[b] Printing the value of d is 10
[c] Compile time error
[d] Run time error
//The output here is Complile Time Error..
Can any one explain me the diffrence between the two??
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
You need a default contructor for Class A, like this:
class A {
A(){ this( any double vale);}
A(double d) {
System.out.println("Printing the value of d is 10");
}
}

Your B class's contructor B(){ .....} calls Class A's default contructor.
its like this. The complier inserts super(); in B(){}

B(){
super();
....
....
}
Ernest Lee
Ranch Hand

Joined: Aug 20, 2001
Posts: 145
the difference is that for the first case.. when subclass of class A (class B) is instaniated, the no-parameter constructor for class A will be called implictly by JVM. (or the constructor for class B calls its super class's constructor explicitly)
Where for the second case, since there is no no-parameter constructor for class A and the constructor in class B didn't call the class A's constructor explicitly, a compile error will occur.
Andre Zhang
Greenhorn

Joined: Aug 14, 2001
Posts: 9
Originally posted by sonir shah:
I came across a question in the mock
It is as under :
class A {
A() {
System.out.println("Class A Constructor");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
// the output here is "Class A Constructor followed by Class B Constructor //
again similar question i came across.It is as under :

class A {
A(double d) {
System.out.println("Printing the value of d is 10");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
[a] Printing the value of d is 10 followed by Class B Constructor
[b] Printing the value of d is 10
[c] Compile time error
[d] Run time error
//The output here is Complile Time Error..
Can any one explain me the diffrence between the two??

Because in the 2nd example, Constructor B() invokes its superclass Constructor A() implicitly. But there's no A() in Class A. this causes a compile time error.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt on constructors