Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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: 72441
315
  • 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:

 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic