Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Originally posted by Jordi Marqu�s:
Hi!
This question is from Sun web:
Consider the following class definition:
1. public class Test extends Base {
2. public Test(int j) {
3. }
4. public Test(int j, int k) {
5. super(j, k);
6. }
7. }
Which of the following forms of constructor must exist explicitly in the definition of the Base class?
A. Base() { }
B. Base(int j) { }
C. Base(int j, int k) { }
D. Base(int j, int k, int l) { }
The answere is A and C but I don�t understand why C is correct because the code will compile wihtout constructor A, because is not necessary.
Thank you in advance
Java lover from hell!
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Originally posted by Shyamsundar Gururaj:
Hello Jodi and Zhang,
Before saying anything, let me state a small preamble...
I am sure that you'll know this already..."Constructors are invoked in the reverse order to the call order".
Now coming to your post...
My understanding of the [b]super() concept says...
super() or this() can be used only in a constructor definition and, if used, must be the first statement in the constructor.
If the first statement in your subclass constructor is neither super() nor this(), the compiler inserts an implicit super() statement to invoke the default constructor of your base class. (Please refer to Michael's post above.)
Now, these properties have certain ramifications...
One is, if the superclass does not define a default constructor but only provides non-default ones, then the subclass constructor cannot afford to have its first statement other than a this() or a specific super() with the right arguments to invoke the matching constructor.
In other words, if the compiler sees that the subclass constructor does not have a super() or a this() construct, it will insert an implicit super() statement. But to its dismay, it'll not find a corresponding default constructor in the superclass causing it to scream profanities at the code
The other ramification is that if the subclass does not have any declared constructors AND the superclass does not have a default constructor, the compiler will definitely not like it as the implicit default super() it generates in the subclass will not find a match in the superclass.
Now, coming to the code that you had posted...
In line 2, your code defines a constructor, it does neither have a super() nor a this() construct. So the complier will insert an implicit super() But assuming that there is no default constructor in the Base class, this will generate a compile-time error as there will be no match for the implicit super() . So, in order for the code to compile, there needs to be a Base() { } statement in the base class.
And of course, the constructor in line 4 will work fine if the constructor Base(int j, int k) { } exists in the superclass.
Hope this helps!
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 03, 2001).][/B]
Java lover from hell!
Don't get me started about those stupid light bulbs. |