Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Superclass Constructor calling from subclass

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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??
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic