• 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 Problem

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,
The following is a question from Simon Roberts
public class Test extends Base
{
public Test (int j)
}
public Test(int j, int k)
{
super(j,k)
}
}
Which of the following forms of constructors must exist explicitly in the definition of the Base Class. The answer says
1) Base() {} // Default Constructor
2) Base (int j, int k) // Constructor with 2 arguments.
I understand the 2nd part, but how can a default constructor exist in the Base Class (becoz if we have a constructor with arguments, then there is no default constructor)???
The answer given is as under
" In the constructor at lines 2 and 3, there is no explicit call to either this() or super(), which means that the compiler will generate a call to the zero argument superclass constructor"
How will it call a zero argument superclass constructor, when it already have a constructor with 2 arguments (becoz if there is no constructor defined, then only there will be a zero argument constructor???)
Please explain the situation (or is my question damm silly and i am missing something?)>>
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<PRE>
public class Test extends Base {
public Test (int j) {

}
.........
</PRE>
When you say
<PRE>
Test t = new Test(69);
</PRE>
the first thing done in constructor <CODE>Test(int j)</CODE> is a call to its super(); i.e., to its default superclass constructor. As you know, if you define as much as one "some-arg" constructor, you must define a no-arg one also, if it's possible that one will be called somewhere. And that is the case here.

 
Venkat01
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tony,
"""the first thing done in constructor Test(int j) is a call to its super(); i.e., to its default superclass constructor. As you know, if you define as much as one "some-arg" constructor, you must define a no-arg one also, if it's possible that one will be called somewhere."""
Why should you define a no-arg one ""ALSO""?? I understand that when you have a arg Constructor, the no-arg Constructor is not provided. The whole concept of mine regarding Constructors has really started shakeing.
My point is that in the base class you have a arg-Constructor and in the derived class you can call the base arg constructor by using super(). Where does the point of CREATING NO ARGUMENT CONSTRUCTOR comes in to picture in the BASE CLASS?
I am sorry, tony, but please explain again.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your example is not complete. I naively pulled the R&H book out and wrote from their example. Please refer to it (do you have the book?), read the complete problem and their explanation for the answer. Then if you have doubts, write again.
If you DO NOT have the book, tell us and I'll reproduce the exercise here. OK?
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another try...
If one writes even one constructor for a class, the compiler won't provide any. So if one needs a default constructor, then, if another constructor with arguments is defined, one must explicitly create a no-arg constructor, because, as I said, the compiler won't.
This may not be a problem in general, but in the example given, it is.
That's because the constructor
<PRE>
public class Test extends Base {
public Test (int j) {
...
}
}
</PRE>
is actually:
<PRE>
public class Test extends Base {
public Test (int j) {
super();
...
}
}
</PRE>
So there has to be a no arg constructor in class Base, in addition to the two int one.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a Constructor(int i) with an Arg explicitly in BASE, You NEED NOT Create a Constructor() with no arg. But, it is a good practice to do it...WHY ??? ...read on....
If you extend the base, and define new constructors ( with different args, say "public child(int, int)") in the child, either you should have the same constructor in the base AND you should call it explicitly ( super(int, int) in the first line...
public child(int, int){
super(int,int)
}
OTHERWISE:
Java will try to put super() (the default) in the above first line..
So, either call the BASE's Constructor explicitly in every constructors in every child....OR ...just explicitly create a default Base() along with the Base(int) and you will not have the headache....
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic