aspose file tools*
The moose likes Beginning Java and the fly likes Superclass Constructor calling from subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Superclass Constructor calling from subclass" Watch "Superclass Constructor calling from subclass" New topic
Author

Superclass Constructor calling from subclass

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
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??


Regards,
Vishnu
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
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

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

Joined: Feb 03, 2010
Posts: 55
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Superclass Constructor calling from subclass
 
Similar Threads
Constructor doubt
doubt in constructor
Can you please clarify my doubt ?
Default Constructor in Sub/Super Class
Construtor Doubt