aspose file tools*
The moose likes Beginning Java and the fly likes Can you please clarify my doubt ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you please clarify my doubt ?" Watch "Can you please clarify my doubt ?" New topic
Author

Can you please clarify my doubt ?

siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
If i write this code as below



I am getting compile time error: constructor superclass in class superclass
cannot be applied to given types;
{
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Actually when we create object s with new keyword and constructor, In that constructor we are passing parameter then only parameterized constructor is invoked no argument constructor is not invoked then why i am getting this error.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

A subclass constructor always ends up calling a superclass constructor. You can call it explicitly (like you have on line 16), but if you don't then the compiler will insert a call to the no-arg superclass constructor. Which means that your code is effectively the same as this:
Now can you see the problem? There is no no-arg constructor in the superclass! The compiler will only insert a no-arg constructor if you don't provide any constructor yourself, but that's not the case here.

(By the way, I'd suggest getting used to following Java conventions, which means that all your class names ought to start with a capital).
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
Matthew Brown wrote:A subclass constructor always ends up calling a superclass constructor. You can call it explicitly (like you have on line 16), but if you don't then the compiler will insert a call to the no-arg superclass constructor. Which means that your code is effectively the same as this:
Now can you see the problem? There is no no-arg constructor in the superclass! The compiler will only insert a no-arg constructor if you don't provide any constructor yourself, but that's not the case here.

(By the way, I'd suggest getting used to following Java conventions, which means that all your class names ought to start with a capital).


Thank you Matthew for your reply i know that compiler places super keyword in no-arg constructor but subclass no-arg constructor is not called right because we are passing value 10 with an object then subclass parameterized constructor is called and from there control passes to superclass parameterized constructor
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

The compiler doesn't just have to worry about how you happen to be using the class in this case. It will make sure that the class can be used in all cases. As things stand, you've added a constructor that can never be called without an error. The compiler knows this must be a mistake, so flags it up.
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

The constructors are called in such a way that the parent constructor is called first and then the flow ends up in calling the child constructor and since you have one constructor in a child class that is non parametrized so the compiler will look for the constructor of the superclass which is also non parametrized and since it is not there so the compiler will throw an error. The solution is either you explicitly call the parametrized constructor of superclass just above line no.12 or define a no argument constructor in the superclass.

Note: Although you are calling the parametrized constructor only but the compiler will check for other combinations also.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you please clarify my doubt ?