File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on constructors

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic