• 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

Help pls about constructors...

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
can somenone pls explain me when we shd provide a null parameter constructor in a class to avoid compile time errors in the subclass of this class.
Thanks in advance
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we dont have to provide the empty parameter contructor in subclass it has to be in superclass, as in subclass, even if we dont insert empty constructor calls, it inserts on its own, and looks for the matching empty parameter constructor in superclass.
hope i've answered ur ques.
 
Gokhan Gultekin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually can u tell me the reason why the following code doesnot compile.
thanks in advance. gokhan
class BaseClass{

public BaseClass(String s){
System.out.println(s);
}

public BaseClass(int i){
this("I am BaseClass Integer");
}
}

public class SubClass extends BaseClass{

public SubClass(int i){
System.out.println("I am SubClass Integer");
}

public static void main(String[] arg){
SubClass sc = new SubClass();
}
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gokhan,
As you are using inheritance you must provide appropriate arguments to super(). If there is no call to super with arguments (as in your case) the default parent constructor (that is, the constructor with zero arguments) is called implicitly. This is the what is causing your error - there is no default parent (Base) constructor.
One additional problem is that your SubClass constructor calls for no arguments and a couple of lines above you have declared
public SubClass(int i)....
I guess that's the long winded way of saying what g Kishnan said above
Hope that helps
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid this type of error, you should always provide a no-argument constructor if both of the following conditions are true:
1) Your class is not final (i.e., it can be subclassed);
2) You have provided any constructors with arguments
If you don't provide any constructors at all, a default no-argument constructor will be generated automatically, so no error would occur in this case.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
In Java,chaining of constructors always occurs from subclass to superclass.Subclass should invokes cons. of its superclass and so on(i.e.upto root of class)So you must provide a way for chaining constructors if their is non default cons. in superclass.
------------------
 
Gokhan Gultekin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks eveyone, it is clear now....
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The original post said the code gave an error. But if no default constructor is supplied for the BaseClass, then shouldn't one be automatically generated? (and hence, no error?)
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lee, if you have constructor with argument(s) then you HAVE to provide contructor without the argument or in other words Default contructor. Java will not insert for you.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM will provide a default constructor if and only if NO explicit constructor (no-arg or with arguments) is provided.
 
reply
    Bookmark Topic Watch Topic
  • New Topic