• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt regarding overloaded constructor

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic