• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

constructor question

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
crystal clear valentin, thanks.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic