Two Laptop Bag*
The moose likes Beginning Java and the fly likes Doubt regarding overloaded constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding overloaded constructor" Watch "Doubt regarding overloaded constructor" New topic
Author

Doubt regarding overloaded constructor

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Please have a look at the following code snippet:



The compiler is complaining that Implicit super constructor Marmaduke() is undefined for default constructor. Must define an explicit constructor Why is it so? When I add a default constructor in the super class Marmaduke, it is happy. What is going on here? The compiler is acting as though the first line of the super class parametrized constructor is a call to this(). Is that true? Isn't it the case that the first statement inside a constructor is a call to super()?

~ Mansukh
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Mansukhdeep Thind wrote:The compiler is acting as though the first line of the super class parametrized constructor is a call to this(). Is that true? Isn't it the case that the first statement inside a constructor is a call to super()?

The problem is that you haven't put a constructor in your Fern class so the compiler has added a no-arg constructor and the first line of this constructor will be a call to super() i.e. it is trying to call the no-arg constructor of the Marmaduke class that does not exist.


Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Oh man. Why couldn't I think of that? I must be really tired. Went for a 90 km bike ride today. I must give my body some rest. Thanks Jo. And good night..
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Joanne Neal wrote:
Mansukhdeep Thind wrote:The compiler is acting as though the first line of the super class parametrized constructor is a call to this(). Is that true? Isn't it the case that the first statement inside a constructor is a call to super()?

The problem is that you haven't put a constructor in your Fern class so the compiler has added a no-arg constructor and the first line of this constructor will be a call to super() i.e. it is trying to call the no-arg constructor of the Marmaduke class that does not exist.


But that default constructor of Fern is not the issue here. Even if the compiler is inserting it for me, it is not getting invoked at all. So that should not cause any issues I believe. I am calling the overloaded constructor of the super class Marmaduke.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Mansukhdeep Thind wrote:But that default constructor of Fern is not the issue here.

Sure it is.

Even if the compiler is inserting it for me, it is not getting invoked at all. So that should not cause any issues I believe. I am calling the overloaded constructor of the super class Marmaduke.

What overloaded constructor? You haven't written one; and the one that Joanne told you about doesn't overload, because it has a different signature. Furthermore, you're getting a compiler (ie, a syntax) error. If the JVM ran into problems running your program, you'd get an execution error.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Yes Winston. I agree with your criticism. The constructors are inserted and resolved by the compiler right up the inheritance tree of the classes. I was thinking on the wrong lines. I was getting ahead of myself and thinking at Run Time while the issue is a compile time problem. Silly of me.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt regarding overloaded constructor