Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

question about 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, fellow ranchers:
I have questions about the follwoing code:
class base {
base(){} //causing error if commented out
base(int a, int b){}
}
public class test2 extends base{
public test2(int i, int j)
{
super(i,j);
}
}
It says that the compiler will generate a call to the 0 argument superclass constructor if there is no explicit this() or super() at the start of child class. Why do we need to put this o argument
constructor in superclass? Since base class here
is actually a subclass of Object, why Object does not provide this 0 argument constructor?
thanks
victor
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
victor,
Since you provide a super() that takes two int integer parameter in the test2 constructor, and there is indeed a base constructor takes two int parameter in your base class, you dont need 0 argument base constructor and compiler should not be complaining about no 0 argument base constructor.
I commented out the base() {}, your code complies OK.
 
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
My bad, i left out one extra line in child, now,
it will give you a starnge error...
I have questions about the follwoing code:
class base {
base(){} //causing error if commented out
base(int a, int b){}
}
public class test2 extends base{
test2(int k){}
public test2(int i, int j)
{
super(i,j);
}
}
I don't know why it is so?
victor
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
victor,
the problem now come from the
test(int i){}
constructor you added in.
keep in mind "the contruction of subclass can not be start till the conturction of superclass is done"
when compiler check the above line, since you do not provide either super() or this() in the test(int i) {}, one 0 argument superclass contructor will be inserted implictly then the code become
test(int i) {
super() // inserted by compiler implictly
}
Then when you commented out base(){}, you dont have any 0 argument constructor in your base definition...compiler flag the error.
hope this help.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first answer to your question still holds true.
The constructor in the derived class, which takes only one argument, has no explicit calls to the constructor with two arguments, either in the ancestor or in the derived class.
Therefore, the compiler will put an implicit call to super() - without any arguments. If it does not find such a constructor, it will give an error at compile time.
If you change the single argument construcot to use any one ( and only one at a time) of the commented lines then you can comment out the line that is causing you an error

 
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

Originally posted by chichih Lin:
victor,
keep in mind "the contruction of subclass can not be start till the conturction of superclass is done"

hope this help.


Thanks for the reply. So, basically, every constructor in child classes need to implicitly
or explicitly call a superclass constructor, right?
Another thing is: base class is actually a subclass of Object, does every constructor of base class implicitly call an Object constructor?
Just want to dig a little deep
victor
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does!!! Object is the mother class of all class, so whenever you create an object (call to a constructor) you make a call to the constructor of Object.
 
Those are the largest trousers in the world! Especially when next to this ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic