• 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

Superclass Constructor calling from subclass

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in it. It was given that if we do not use any super statement for calling a constructor of superclass in subclass, a superclass constructor with no arguments is called implicitly. But a compile time error occurs, if superclass does not has a constructor defined with no arguments.
My doubt here is when a default constructor is there for each class, and this default constructor has no arguments, then why this compile time error occurs??
Do we need to explicitly define the default constructor also in super class??
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Sharma wrote:I have a doubt in it. It was given that if we do not use any super statement for calling a constructor of superclass in subclass, a superclass constructor with no arguments is called implicitly. But a compile time error occurs, if superclass does not has a constructor defined with no arguments.
My doubt here is when a default constructor is there for each class, and this default constructor has no arguments, then why this compile time error occurs??
Do we need to explicitly define the default constructor also in super class??



Yes. That's true. We don't have to define constructor explicitly.



My above code is not giving any kind of compliation error. So, can you post your code so it will be better to guide you???

Thanks
James
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A default constructor is supplied by the compiler if and only if you do not write a constructor yourself. If you want a no-arguments constructor, you write one; if you don't, write a constructor with arguments and the compiler will not supply a default constructor.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:A default constructor is supplied by the compiler if and only if you do not write a constructor yourself. If you want a no-arguments constructor, you write one; if you don't, write a constructor with arguments and the compiler will not supply a default constructor.



Ohk. That means either do not write any construtor or if you want to write, write for each case. The summary is, default construtor will not be provided if we are writting the construtor. Please correct, if its wrong.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will only generate a public no-arguments constructor when you don't add any constructor at all to your class. So if you add any constructor, doesn't matter if it takes arguments or not, the compiler will not add a no-args constructor automatically.

You don't need to call the constructor of the superclass explicitly in the constructor of your subclass. You can if you want to, and if you don't, the compiler will implicitly add a call to the no-arguments constructor of the superclass. For example:

The compiler will automatically add a super(); call to the constructor of the subclass.

Note that if the superclass does not have a no-arguments constructor, then you are required to call the superclass constructor explicitly - the compiler will not automatically add a call to a superclass constructor that has arguments. The following example will give you a compiler error:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic