Explanation regarding a constructer
I came accros a SCJP MCQ that is to get the out put of a simple programe.
class A{
public A(String s){//line X
System.out.println("Prints A");//line Y
}//line Z
}
publicclass Bextends A{
public B(String s){
System.out.println("Prints B");
}
publicstaticvoid main(String args[]){
new B("C prints");
}
}
When i compiled the code it gives out a compilation error.I have commented the line x,y,z which will then the code will compile.Can any genius give me a explanation why code didn't compile in the first place and after comenting why did it compile.
[1413 byte] By [
ram102125a] at [2007-11-27 6:57:01]

2.1 and 1.2.
Constructor rules:
1) Every class has at least one ctor.
1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}
if you want one.
1.3) Constructors are not inherited.
2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)
or a call to another ctor of this class this(...)
2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()
as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.
jverda at 2007-7-12 18:34:10 >

Add super("any string") to B constructor and the problem will be solved.
class A{
public A(String s){//line X
System.out.println("Prints A"); //line Y
}//line Z
}
public class B extends A{
public B(String s){
super("hi!"); //add this line.
System.out.println("Prints B");
}
public static void main(String args[]){
new B("C prints");
}
}
gssa at 2007-7-12 18:34:10 >

> Add super("any string") to B constructor and the problem will be solved.
I don't think so.
First because this will result in a spurious "Prints A" being printed. But more importantly because that ("How do I make it compile?") wasn't the problem. The problem was: Why does it compile with the superclass constructor commented out, but not otherwise?
And the solution is in reply 2 (and, I suspect, in the compiler message refered to in reply 1).
I see none of you are fluent in foreign newbenese, otherwise you would have realised his actual question was: "I compiled this code and got an error, then I added in the three comments and it worked for some reason....what up with that?"
The answer to which is, it probably has nothing to do with your three comments, it's probably because of something you did that you just don't realise/remember. 'Tis strange though.