Explicitly specify a default constructor in A. Constructors are chained and when you call a constructor of a sub class the super class constructor is run first. Here, As you haven't specified any default constructor in A and yes the compiler won't put any too.
The other alternative is make a default constructor in B and put the first line as super(3) or any int value in super.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
There are a few things to keep in mind
1. The compiler WILL put a no arg constructor if you do not supply ANY constructor.
2. If you DO NOT supply a no-arg constructor but DO supply a constructor with args, the compiler WILL NOT take up the responsibility for the no-arg constructor.
3. The compiler implicitly puts in a constructor with 'super()' in the subclass if you do not provide one for the subclass explicitly.
4. When you instantiate a sublass, the constructors of ALL it's superclasses are invoked automatically (via super()), all the way up to the great-grand-dad 'Object'.
(Please note: The caps in the text above are just to highlight the important aspects, no offense meant.)
So in your first example, the class A had a contructor with integer args but did not have a no-arg constructor. When you compiled A.java, the compiler did not put in a no-arg constructor.
Think of it this way. 'B extends A' means that when you try to instantiate B, the constrcutor of B is invoked, which inturn invokes the constructor of it's superclass (i.e. 'A'). But 'A' has only one constructor that takes an integer argument (remember the compiler doesnt put a no-arg constructor in this case). But how is that possible if you dont supply anything to A's constrcutor? That was the reason the compiler raised an objection.
Incase you're wondering there's two ways this would work
Hope this helps. Enjoy!
Joined: Jun 30, 2009
Thanku for this fantastic explanation.
I am very clear with the concept now.
But can you please explain the following??
Suman Poluri wrote:'B extends A' means that when you try to instantiate B, the constrcutor of B is invoked,
I guess instantiate means
How B extends A would be an instantiate???
So there is no instance of B called still it calls super()???
that means while compiling it invokes constructor & not at ??
the code below just compiles as above code throwing error.
Look when you say B extends A means now we can inherit all the things of A into B but Constructors are never inherited. Also if you create an instance of B say B b = new B(); N no constructor is written over there then the compiler puts a default constructor and the first line of the constructor is always to super(). It is implicit. It is either super() or this(). this() is explicit representation but super() is implicitly put. So when you instantiate an instance of the sub class the Constructor of the sub class is invoked which calls the super class's default constructor which is not there only. Why its not there because the you have already written down a constructor and so the compiler will not put a default no arg constructor. So the call to super() from the sub class constructor fails. So the B instance won't be created because its constructor execution fails.