• 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

default constructors and constructors

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the default constructor for a subclass takes no parameters and invokes the superclass constructor with no parameters, then the superclass must have a constructor that takes no parameters, otherwise a compile-time error occurs.
what about an explicitly defined constructor in that subclass with, say, two parameters? Does it, like the default constructor for the subclass, attempt to invoke a similar constructor in the superclass?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If by a "similar constructor" you mean the "constructor that takes no parameters", then, as Marilyn says yes.
But if you mean it invokes a constructor in the superclass with two arguments, then no.

Fixed nutty thinking mistake. "two constructors" -> "two arguments". Thanks Dirk
[ January 15, 2003: Message edited by: Barry Gaunt ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Barry meant, "But if you mean it invokes a constructor in the superclass with two arguments, then no."
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same question actually and I thought that the answer might be that what is written above, which is that
calling a local constructor that has two arguments doesnt call the constructor in the superclass that has two arguments but rather calls the one without arguments.
Anyway Im glad to know I was right
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey! Thanks for all the replies so far!
Why would a 2-parameter constructor in a subclass implicitly call the superclass' default no-parameters constructor?
What happens then if there is no default no-parameters constructor?
eg.
class Base{
Base( int A ) throws exception
}
class A extends Base{
\\ what happens to the implicit call to Base
\\ here?
A( int a , int b ) {
}
}

thanks in advance
[ January 15, 2003: Message edited by: Jasper Vader ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile it and see...
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i could only get this to work...
class Base{
Base() {
}
}
class A extends Base{
// what happens to the implicit call to Base
// here?
A( int a , int b ) {
}
}
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this also compiles...
class Base{
Base() {
}
}
class A extends Base{
// what happens to the implicit call to Base
// here?
A( int a , int b ) throws Exception{
}
}

Of course, if i swap it around so that Base constructor throws an Exception, compiler complains that the class A constructor doesent throw an exception.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But when i try to compile without a no-parameter constructor present in Base, this is what the compiler says...
"cannot resolve symbol
symbol : constructor Base ()
location: class Base
A( int a , int b ) {
^
1 error
"
in response to...
class Base{
Base( int a , int b ) {
}
}
class A extends Base{
A( int a , int b ) {
}
}

I am trying to understand this.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh okay, constructors can not be overridden. that's why the last example would not compile. So i tried it with 3 paramters in the class A constructor, nup would not work. Left the 3 parameter constructor in class A and changed the Base constructor to no parameters and hey presto it worked.
I surmise: that any class that is going to be subclassed must include a no-parameter constructor as one of its constructors.
Question: Why does a 3 parameter constructor need to call the no-parameter constructor?
 
Shashank Gokhale
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper,
Every class has a constructor, and a default one (constructor with no parameters) is automatically provided by the compiler unless a constructor is created by the programmer. So, in the clas
class cat
{
}
a default constructor
cat()
{}
is provided by the compiler
For the class
class dog()
{
dog(String name)
{}
}
no default constructor is provided by the compiler because at least one has been created by the programmer, even if a constructor created by the programmer takes no parameters.
So in response to your question, 'What if a default constructor isnt provided?', understand that a constructor always exist (created by the programmer or provided by the compiler).
If a subclass object has to be created, then regardless of how many parameters are involved, the default base constructor is always called.
So in
class cat
{}
class dog extends cat
{
dog(int age, int numOwners)
{}
}
the default constructor of cat is implicitly called because a cat object has to be created before a dog object can.
Also, one of the rules on subclassing is that a subclass can have exceptions lesser or equal to the base class but not more.
Therefore the class below will compile and run.
class Base{
Base( int A ) throws exception
}
class A extends Base{
\\ what happens to the implicit call to Base
\\ here?
A( int a , int b ) {
}
}
In the following
class Base{
Base() {
}
}
class A extends Base{
// what happens to the implicit call to Base
// here?
A( int a , int b ) throws Exception{
}
}
there is no problem in comepiling because neither the base constructor and the default constructor of class A throw an exceptions. The 2 parameter constructor of class A doesnt have any relation to the default constructor or any other constructors in the base class, so it wouldnt matter whether the base constuctor threw an exception or not as far as the 2 parameter constructor of class A is concerned.
Why the compiler would complain if the base constructor did throw an exception, I dont know.
In the following
class Base{
Base( int a , int b ) {
}
}
class A extends Base{
A( int a , int b ) {
}
}
the compiler gives you the error saying it cannot resolve the base() constructor, because there isnt one now. By creating a constructor, you have prevented the compiler from providing a base() constructor, and you havent created one either. Without a default base() constructor or a base() constructor created by you, the compiler has no way to create a base object first and then build on it to create an A object.
Your last question was why would a 3-parameter subclass constructor need to call the defaultconstructor in the superclass. The answer is that the no parameter constructor of the superclass is always implicitly called to create a super class object before a subclass object can be created.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always
the no arguments of superclass's constructor will be called in the beging of the subclass's constructor. Recursevely, the no arguments constructor of Class Object will be called always!!!
try:

public class teste extends KKK{
public static void main(String args[]){
new teste();
new teste(2,3);
new teste(2,3,4);
}
public teste(){
System.out.println("inside teste1");
}
public teste(int a,int b){
System.out.println("inside teste2");
}
public teste(int a, int b,int c){
System.out.println("inside teste3");
}
}
class KKK{
public KKK(){
System.out.println("KKK1");
}
public KKK(int a,int b){
System.out.println("kkk2");
}
}
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for the input guys!
I do understand that by creating a constructor for a class the default no-arguments constructor is prevented from being made... I am rather intrigued though that I had never read anywhere that in order for a class to be subclassed, a no-parameter constructor MUST be included in its constructors list.
Or maybe i did read it somewhere and forgot. :roll:
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a no-arg constructor if you want to subclass - the no-arg constructor *only* gets called implicitely if no other constructor call is given.

Hope this helps.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To perhaps summarize and clarify the lesson learned here:
Excepting perhaps the constructors in the Object class, the first thing any constructor must do is invoke either a parent class constructor with super(appropriateArgs) or invoke a constructor in the same class with this(apporpriateArgs). As Ilja pointed out, if the source code doesn't explicitely make such an invocation, then the compiler fills it in with a call to the default constructor in the super class. (Not unlike when the compiler creates a default constructor in a class when no other constructor was defined.)
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
then the compiler fills it in with a call to the default constructor in the super class.


Just I minor correction regarding terminology:
The default constructor is the one created by the compiler if no other constructor is implemented. The default constructor always has the empty implementation.
What gets called above is the no-arg constructor of the super class - which might be either the default constructor or an explicitely implemented custom no-arg constructor.
Well, I hope I didn't add to the confusion... :roll:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In OOP terminology, a default constructor is one with no arguments, afaik. The one created by the compiler could also be termed a default constructor, but I think I would avoid that terminology. It may be longer, but you can call it "the constructor automatically created by the compiler" or some such.
Just my $0.02
Layne
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for a little more clarification... at Sun Education, in Java, we usually *do* use the terminology Ilja mentioned, to make it simple.
The Java spec uses the term, "the default constructor" to *always* mean "the one provided by the compiler" (which of course is always no-arg). You *could* still say that you have "A" default constructor, which doesn't necessarily mean it is "THE" default constructor, but to keep it clear, we have adopted the convention of making a distinction between "the default constructor" and "a no-arg constructor".
We do this, I believe, because we're too lazy to use the rest of the sentence, "the default constructor provided by the compiler" vs. "the default constructor YOU wrote".
It's just simpler to say "default" to mean "compiler-generated" and "no-arg" to mean either human OR compiler-generated.
And though it might not matter much in this forum, the use of the terminology I've described here is the way we refer to it in the Programmer certification exam.
Cheers,
Kathy
implements Lazy
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy,
By 'implements Lazy' i understand that you may be associated with, but not necessarily an instance of, lazy. Only in a good way, of course .
Talking of being lazy in a good way, i think that allowing the compiler to generate a no-args constructor is great, but if one creates a constructor for a class then there is going to be no no-args constructor unless it is explicitly defined.
My assumption is that if a class is to be subclassed, then it MUST have a no-args constructor either implicitly or explicitly defined in it, so that the subclass can call that no-args constructor (with whatever constructor that subclass might be using at the time, even an args constructor).
I just wonder why this is.
I guess i am referring to a simple bit of code posted by Mr Gaunt...
class Base{
// there is a default constructor
// Base() provided by the compiler here.
}
class A extends Base{
A( int a , int b )
{
// there is an implicit call
// to Base() here.
}}
[ January 18, 2003: Message edited by: Jasper Vader ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jasper Vader:
My assumption is that if a class is to be subclassed, then it MUST have a no-args constructor either implicitly or explicitly defined in it, so that the subclass can call that no-args constructor (with whatever constructor that subclass might be using at the time, even an args constructor).
I just wonder why this is.


Well, it's not correct - the following code should compile without problems:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, well, yes! silly me, thankyou Ilja. Sorry about my slowness on this topic. A call of 'super' and say one parameter in brackets will call the one-parameter constructor in Base.
But in the code below, there is a new Sub being constructed with a one arg parameter and i just don't understand why it will not compile when i remove the no-arg constructor from Base (i show the code with it included).
Why is a no-arg constructor implicitly called in this situation?
class Base{
Base(int i)
{}
Base()
{}
}
class Sub extends Base
{
Sub()
{}
Sub(int i)
{}
public void makeNewSub()
{
Sub s = new Sub(0);
}
}
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jasper,
The no arg contructor is being called because you did not explicitly call the args constructor in super class by using the super keyword.
your code is
Sub(int i)
{}
If you change it to
Sub(int i)
{
super(i);
}
then, instead of the no-arg super constructor, the superclass constructor that takes in an integer is called.
Hope this helps
Sri
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:

The no arg contructor is being called because you did not explicitly call the args constructor in super class by using the super keyword.



Hi Sri yes, i am confused by this behaviour, i am wondering why it happens - maybe i should ask the ppl who designed java!
Anyway, thanks for the code example, always helpful.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:
If you change it to
Sub(int i)
{
super(i);
}
then, instead of the no-arg super constructor, the superclass constructor that takes in an integer is called.


Ohhh okay, there is an implicit call to super() within a subclass' method body unless one explicitly calls super, eg super(0).
I get it now thanks everyone
[ January 19, 2003: Message edited by: Jasper Vader ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper, you meant constructor right?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ow! ... what can i say ... DOH!
I hope there are no Doh moments for me on the Java exam. Or rather should I say, I hope there are no undetected Doh moments when i submit the answers :-).
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kathy Sierra:
Just for a little more clarification... at Sun Education, in Java, we usually *do* use the terminology Ilja mentioned, to make it simple.
The Java spec uses the term, "the default constructor" to *always* mean "the one provided by the compiler" (which of course is always no-arg). You *could* still say that you have "A" default constructor, which doesn't necessarily mean it is "THE" default constructor, but to keep it clear, we have adopted the convention of making a distinction between "the default constructor" and "a no-arg constructor".
We do this, I believe, because we're too lazy to use the rest of the sentence, "the default constructor provided by the compiler" vs. "the default constructor YOU wrote".
It's just simpler to say "default" to mean "compiler-generated" and "no-arg" to mean either human OR compiler-generated.
And though it might not matter much in this forum, the use of the terminology I've described here is the way we refer to it in the Programmer certification exam.
Cheers,
Kathy
implements Lazy


Thank you for the clarification. Terminology can be a little confusing, especially when a single word can be used in several different ways.
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic