• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
This question is from Sun web:
Consider the following class definition:
<pre><>
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. }
</pre>
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 without constructor A, because is not necessary.
Thank you in advance

[This message has been edited by Marilyn deQueiroz (edited September 03, 2001).]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have calls to two different constructors in Test. The first one you can't see but it's always implied.

You don't need to include a constructor in Base in this example. But adding a non-default constructor in Base changes things. The default constructor is not implied when an explicit constructor exists. Thus while the super(j,k) call would work, the implied super() call would break without adding the default constructor in.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if we need do something in the super class's constructor,then the default constructor is needed or the default constructor need not be explicitly defined.I don't
know the reason why answer A is correct?

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!
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Metal,
The reason why A is correct that in every constructor there is a implicit call to super constructor ( Super()) if you dont expilicitly call any constructor from super class or the same class (e.g. this() ). As the super class should have super(int j, int k) which means the default constructor is not provided by the compiler hence the Super class should have explicitly declare the no argument constructor.
HTH.
--Farooq
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jordi 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 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 cause 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).]
 
Metal Zhang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O,I see.Your explanations are quit good.Thank you all!

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!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shyam,
That was a wonderful , wonderful explanation.
Thanks a lot.

[This message has been edited by Bindesh Vijayan (edited September 04, 2001).]
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There are 2 rules about Constructors that this questions wants us to understand :

  1. The first statement of the Constructor is always a call to the default constructor of the super class using the implicit super() statement.
  2. If you have a overloaded constructor then the default constructor is not available implicitly

  3. Applying the first rule to the question, the compiler would treat the code as :
    <pre>
    1. public class Test extends Base {
    2. public Test(int j) {
    2.5 super() //added by the compiler
    3. }
    4. public Test(int j, int k) {
    5. super(j, k);
    6. }
    7. }
    </pre>

    This means for the line 2.5 , you should have a default constructor in the Base class.Also, for line 5, you should have a constructor Base(int, int) in the super Base class.
    Hence the answer is A and C.
    Now, let us see how rule 2 is applicable here.For statement 5, as mentioned above, you should have Base(int,int) as a overloaded constructor in the Base class.So C is definitely correct.
    But why A?Why do we need to define default base constructor - Base() for superclass Base?Even though the Test(int) is implicitly calling Base() using super(), we also know that every class has a default constructor which is implicitly provided.
    So, will Base() be implicitly provided?No..because the default constructor is implicit if and only if there are no overloaded constructors in that class.In this case, we already have a overloaded constructor in the form of Base(int, int).So, we have to make an explicit mention of default Base Constructor - Base().
    Hence, A is required!
    Hope this helps,
    Sandeep
    SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic