• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Constructors

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the effect of compiling the following code and then executing the 'Child' application?
1. public class Papa {
2. int i;
3. Papa(int j) {i=j;}
4. }
5.
6. class Child extends Papa {
7. Child() {i=5;}
8. public static void main(String[] args) {
9. new Child();
10. }
11. }
A. Compiler error at line 6.
B. Compiler error at line 7.
C. Compiler error at line 9.
D. The code compiles but throws exception when the application is executed.
E. The code compiles and executes with no problems.
The answer is B. I can understand this. Since 'Papa' has non-default constructor, it does not get a no-parameter constructor at compile time. So line 7 will not compile, as the compile tries to call its superclass default constructor.
My question is: Will it compile if line 7 was:
Child(int k) { // do something
}
Does the compiler try to call the superclass no-parameter constructor only if the Child class has a no-parameter constructor?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I think it will not. Try the code to confirm. Just changing the constructor to
Child(int j) {i=5;} will not remove the compiler error. Since the base class constructor takes an arg. and the base class does not have no-arg constructor, You have to explicitly call that,
super(6);
So the new form of Child constructor is:
Child(int j) {
super(j);
i=5;
}
The above is one way to remove the compiler error.
The other way is by giving the base class a non-arg constructor.
Please somebody tell if I am wrong.....
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Parimala,
An easy way to think of it is:
JVM:
All class constructors will have a first line that contains a call to the method super().
Therefore, if you don't type it in then the compiler will place the following line as the new first line in ALL class constructors:
super(); // New first line provided by your friendly Java compiler!
Regards,
Manfred.
 
Parimala Somasundaram
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred, Jini.
 
reply
    Bookmark Topic Watch Topic
  • New Topic