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

Doubt regarding overloaded constructor

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10502
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic