• 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

One question about constructor

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the java code as follow.
----------------------------
class Parent{
Parent(int i){
System.out.println("Constructor in Parent with one argument!");
}
}

class Child extends Parent{
Child(int i){
//super(i);
System.out.println("constructor in child with one argument!");
}
Child(){
System.out.println("constructor in Child without argument!");
}
Child(int i, int j){
System.out.println("constructor in Child with two arguments!");
}
}
----------------------------
There are compile error with subclass's constructor.
Would somebody can tell me why?

And if I add the default constructor in the Parent class, ther will no error. Why?

Thanks!
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frm Child constructor always first call is no-arg constructor of superclass. Since u have already declared one constructor in parent, compiler will not create a default no-arg for u.

Either
1. comment Parent class constructor
or
2. add no-arg constructor.


Naseem
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naseem Khan:
Frm Child constructor always first call is no-arg constructor of superclass...


...unless the first line of the Child constructor is an explicit call to another Child constructor (using "this") or an explicit call to a Parent constructor (using "super"). In other words, it's only when one of these explicit calls is absent that an implicit call is made to the no-args superclass constructor.

And as Naseem pointed out, a no-args constructor is automatically provided by the compiler only if no other constructors are explicitly written into the class.
[ May 17, 2006: Message edited by: marc weber ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah you are absolutely right marc.

Thanks for correcting me.

Naseem
 
Ling Mike
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see!
Thanks very much!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic