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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have basic question in overloading constructors. Why do I need to create a default constructor in Basedemo. I have a call to default super constructor(super) in one argument Basedemo constructor. Shouldn't super() call the super default constructor?. Why do i need to create a no arg constructor?
If I didnt create default no arg constructor as given in the code below, it gives compiler error.



[ August 26, 2004: Message edited by: vijaya dev ]
[ August 26, 2004: Message edited by: vijaya dev ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default constructor is created implicitly by the compiler ONLY if you don't provide any other constructors in the class.

In your example, since you've created Basedemo(int y) constructor then the compiler will not create the default constructor, therefore the implicit call that is made from Derived(int x,int y) constructor will fail.

Hope this help.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Vicken said, keep in mind that if you do not explicitly make a call to super from a constructor, the superclass' no-args constructor will be automatically called on the first line. That being said, you can avoid having to declare a no-args constructor by making an explicit call from Derived(int x,int y) to a defined constructor in the superclass (exactly like you did in Derived(int s)).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic