Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors are not inherited?

 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have read that constructors are not inherited... although i have referred so many for this i could not get the point right. if constructors are used for declaring and initialising the variables, then will there be any use inheriting the class without constructors.???
please hint me on this...
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somehow a constructor of a class that you inherit from is called at a certain point that has to do with construction.

Vague hint is vague?
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops.. Ronald i should have told like 'please explain me'
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I didn't mean that you're vague, you asked for a hint and I gave one that I thought was vague. I guess it was.


If you inherit a class, if you do not explicitly call the constructor of the super class in the sub class constructor, the compiler does this for you.

Up to you to find out what this means, and what the compiler does.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iif you wish to explicitly use the supercalss constructor you must make a call to super. in your subclass, if not your subclass will either get a default constructor or you will have to create a non-default construcor.
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think that will happen if you try to compile this ...



if you don't know the answer, just read about "default constructors"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic