• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Contructor "good or bad"

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks !
I have a problem and doubts with constructors.
does any one tell me why constructor with parameters cannot be inherit ???remember the fact that default constructor can inherit.

this works well.
But Not Working Explain with Details ....!!!
 
Greenhorn
Posts: 19
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt even first one works well ..


It is not a constructor , eventually a valid java method which happens to be a same name as a class name .... remember constructors doesn't have return type, not even void .

PS : In my opinion constructors cant be inherited , be it default one ...
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what's going on...

You aren't specifying a constructor for the B class. So the compiler inserts a default constructor, taking no arguments.

Because B is a subclass of A, any constructor of B must call a constructor of A. Again, if you omit this, the compiler will insert the call for you. But it can only insert a no-arg constructor like this. In the second case, there isn't a no-arg constructor. So if you want to inherit from B in that case you need to put in the constructor calls explicitly. For instance, like this:



Actually, writing that I've realised there were two problems. Your examples didn't have any constructors. They were ordinary methods - constructors don't have a return type.

 
Vxyz eom
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vibhor sharma wrote:I doubt even first one works well ..


It is not a constructor , eventually a valid java method which happens to be a same name as a class name .... remember constructors doesn't have return type, not even void .

PS : In my opinion constructors cant be inherited , be it default one ...


Now is it correct Vibhor ???
 
Vxyz eom
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Here's what's going on...

You aren't specifying a constructor for the B class. So the compiler inserts a default constructor, taking no arguments.

Because B is a subclass of A, any constructor of B must call a constructor of A. Again, if you omit this, the compiler will insert the call for you. But it can only insert a no-arg constructor like this. In the second case, there isn't a no-arg constructor. So if you want to inherit from B in that case you need to put in the constructor calls explicitly. For instance, like this:



Actually, writing that I've realised there were two problems. Your examples didn't have any constructors. They were ordinary methods - constructors don't have a return type.


Thanks Matthew You are Correct.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic