aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor question" Watch "constructor question" New topic
Author

constructor question

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

issues 2 errors:
1. Wrong number of arguments in constructor
2. No constructor matching SuperClass() found in class SuperClass
In the first instantiation we're only instantiating the SubClass
SubClass s1 = new SubClass( "The" );
which doesnt have the correct constructor which should be wrong
In the second instantiation, however
SuperClass s = new SubClass( "The" );
we're creating a SubClass object of SuperClass type which does have the correct constructor
Also I want to be sure that if a subclass extends a superclass, but the subclass only has the no args default constructor, as in this example, will the compiler ALWAYS try to invoke the superclass constructor?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The superclass ctor is always invoked (except in the case of the class Object ). If you don't invoke one explicitely then the default no-arg ctor of the superclass will be invoked.
The actual object you create (here SubClass) is the one whose ctor is invoked first. In this case, we don't have any ctor taking a String as argument in SubClass, hence the error.
Moreover, the ctor is not invoked by the compiler but by the interpreter at runtime. The compiler just makes sure that it will be possible at runtime for the interpreter to invoke the ctor by investigating if the ctor the programmer has written effectively exists. In clear, the compiler looks in SubClass if there exists a ctor taking a String argument. Well, it doesn't, so let's choke!
HIH
[ January 29, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
crystal clear valentin, thanks.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
when constructor calls are made, first a call to super() is made. this is inserted by the compiler. what this does is call the constructor for its superclass. In your code the super-class does not have a default constructor, meaning the one with no args.
if you add that in your super-class it shall be ok. and second you need an appropriate constructor in your sub-class that you have, meaning the one with String args.
so you need two ammends in your code.

Originally posted by Paul Salerno:

issues 2 errors:
1. Wrong number of arguments in constructor
2. No constructor matching SuperClass() found in class SuperClass
In the first instantiation we're only instantiating the SubClass
SubClass s1 = new SubClass( "The" );
which doesnt have the correct constructor which should be wrong
In the second instantiation, however
SuperClass s = new SubClass( "The" );
we're creating a SubClass object of SuperClass type which does have the correct constructor
Also I want to be sure that if a subclass extends a superclass, but the subclass only has the no args default constructor, as in this example, will the compiler ALWAYS try to invoke the superclass constructor?
Ruchi Kolla
Greenhorn

Joined: Jan 24, 2002
Posts: 19
In the second instantiation, however
SuperClass s = new SubClass( "The" );
we're creating a SubClass object of SuperClass type which does have the correct constructor
Also I want to be sure that if a subclass extends a superclass, but the subclass only has the no args default constructor, as in this example, will the compiler ALWAYS try to invoke the superclass constructor?

Here is the code it will work

The output is:
Inside SuperClass without args
Inside SubClass with args
When the SuperClass has any argumented constructor, it is necessary that we should define no argument construtor for it. the compiler always try to invoke the superclass constructor with no args unless specified like:

In this case it will call the argumented Constructor of SuperClass and we don't need no argument constructor in SuperClass.
But if we are not calling argumented constructor of SuperClass, the compiler will try to invoke no argumented constructor of SuperClass even in argumented constructor of SubClass.
 
jQuery in Action, 2nd edition
 
subject: constructor question