• 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

default constructor in superclass

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A1 a1 = new A1("string passed"); // in subclass.
will the above code 'always' invoke the superclass's default constructor even if default constructor in superclass is explicitly coded or not ?
I just need the answer to clear my doubts , or for any reason it does not. I am trying executing.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
it will invoke the superclass constructor which take the string argument, and not the default constructor.
this is called chaining of constructor.
hope this helps
ALkesh
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I can't agree with the first reply.
It should be the default constructor of supclass to be called implicitly by every constructor of the derived class. If the default constructor not exist in super class, there will be compile error. Try the following code:
class Base {
Base(int i) {}
}
class Derive extends Base {
Derive (int i){}
}
there will be compile error: No constructor matching Base() found in class Base. You can correct the code by delet the Base(int i){} in class Base, so the compiler will make a default constructor for Base.
If there is anything wrong in my statement, please feel free to correct me. Thanks
 
Linda Xu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just add one point to my previous reply:
If you called super(param list) in the constructor of derived class explicitly, it will not call super class's default constructor any more. The following code compile and run correctly too:
class Base {
Base(int i){}
}
class Derived extends Base {
Derived(int i){
super(i);
//some other valid code
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic