Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Constructor

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



its giving a compiler error. when i added a default constructor in
animal class it worked fine. What is happening here
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your dog class, you don't specify any constructor. By default, the no-arg constructor will be created and it will call super() implicitly. Since you don't have no-arg constructor in your Animal class, calling super() will make the compiler complain.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply wong

The constructor being called is of animal so the control should pass to animal constructor which is there. I didnt understand the control flow and when compiler would add the default constructor during class load or at the time of compile. can you please throw some light on it... I am unable to understand the behaviour of compiler regarding adding default constructor at what time constructors are checked and added.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe a default constructor is always created unless you specify your own constructor in a class.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks craig for your reply,

my question is when at excatly what time compiler checks and adds this default constructor. And while adding does it check the super class at the same time for a default or a parameterized constructor.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

my question is when at excatly what time compiler checks and adds this default constructor. And while adding does it check the super class at the same time for a default or a parameterized constructor.



Compiler adds the default constructor if and only if you dont have a parameterized constructor (which implies that in the above example, compiler creates default constructor for Dog and would not create for its super class Animal).

Now since a default constructor has always an implicit call to the default constructor of super class (super() as its first statement, but as we already concluded, super class (Animal) has no default constructor in our case. So resulting in a compiler error.

Hope it helps.

Ansar Shah
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ansar,

Let me explain what is my doubt exactly.

suppose assume that i have not commented animal constructor
i am using netbeans IDE. so when i use step into (F7) control starts from new statement in main method and flows to the actual animal parameterized constructor. so my doubt is when did compiler check for default constructor at the class load time?

I just wanted to know at what time compiler checks for the existence of constructors and at what time constructor rules are verified.

I hope this time i have made my doubt clear...

Am I going wrong with IDE usage, i am using it for the first time..
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic