• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why Is a No-Arg Constructor Required Here?

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a mock exam question from CertPal.com. The answer is that the code doesn't compile. The code does compile if I add a no-arg constructor. I don't understand why this is required here.

 
Saloon Keeper
Posts: 15714
367
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Cert extends Marmaduke. Every class has a constructor, even Cert, although its hidden. Every constructor also needs to call a superclass constructor in its first line. If you don't do this, the constructor will secretly call a no-arg superclass constructor by itself. So in this case, Cert's (hidden) constructor attempts to call Marmaduke's no-arg constructor. However, Marmaduke doesn't have a no-arg constructor, because it already has another explicit constructor with a String argument.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help, but I still don't understand.

I'm not trying to create an instance of Cert, so why is its constructor being used at all?

Oh, I guess that I understand. The point is that even though I'm not constructing a Cert, the compiler creates a default constructor for it and in the default constructor, there is a call to super(), so the super-class must have a no-arg constructor.

Thanks. I think that I understand now. Right?
 
Stephan van Hulst
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the answer raises another question as well, why the compiller doesnt make an default constuctor for Marmaduke as well ?

Kaydell Leavitt wrote:Thanks for the help, but I still don't understand.

I'm not trying to create an instance of Cert, so why is its constructor being used at all?

Oh, I guess that I understand. The point is that even though I'm not constructing a Cert, the compiler creates a default constructor for it and in the default constructor, there is a call to super(), so the super-class must have a no-arg constructor.

Thanks. I think that I understand now. Right?

 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Csaba Szegedi wrote:But the answer raises another question as well, why the compiller doesnt make an default constuctor for Marmaduke as well ?



The compiler will only generate a "default" constructor only if there isn't any other constructor. That's why it is called "default". In Marmaduke's case there is another constructor, so the compiler won't create a second one.
 
Ranch Hand
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just few minutes ago i was reading about Rules of Constructors. Let me share that here...hopefully that will solve all the confusions related to it.

Rules for Constructors:

1. Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
2. The constructor name must match the name of the class.
3. Constructors must not have a return type.
4. It’s legal (but stupid) to have a method with the same name as the class, but that doesn’t make it a constructor. If you see a return type, it’s a method rather than a constructor. In fact, you could have both a method and a constructor with the same namethe name of the classin the same class, and that’s not a problem for Java. Be careful not to mistake a method for a constructor, be sure to look for a return type.
5. If you don’t type a constructor into your class code, a default constructor will be automatically generated by the compiler.
6. The default constructor is ALWAYS a no-arg constructor.
7. If you want a no-arg constructor and you’ve typed any other constructor(s) into your class code, the compiler won’t provide the no-arg constructor for you. In other words, if you have typed in a constructor with arguments, you would not have a no-arg constructor unless you type it in yourself !
8. Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler.
9. If you do type in a constructor (as opposed to relying on the compiler-generated default constructor), and you do not type in the call to super() or a call to this(), the compiler will insert a no-arg call to super() for you, as the very first statement in the constructor.
10. A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
11. A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you’re free to put in your own noarg constructor.
12. You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
13. Only static variables and methods can be accessed as part of the call to super() or this(). (Example: super(Animal.NAME) is OK, because NAME is declared as a static variable.)
14. Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
15. Interfaces do not have constructors. Interfaces are not part of an object’s inheritance tree.
16. The only way a constructor can be invoked is from within another constructor.

From the above list, point number (7) is the most relevant asnwer for this particular problem set.

 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks for your answer, i miss understood the meaning of "default", i take it like "always generated" - if it is not created.

Paul Clapham wrote:

Csaba Szegedi wrote:But the answer raises another question as well, why the compiller doesnt make an default constuctor for Marmaduke as well ?



The compiler will only generate a "default" constructor only if there isn't any other constructor. That's why it is called "default". In Marmaduke's case there is another constructor, so the compiler won't create a second one.

 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply for you too nice collection

Faisal Fuad wrote:Just few minutes ago i was reading about Rules of Constructors. Let me share that here...hopefully that will solve all the confusions related to it.

Rules for Constructors:

1. Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
2. The constructor name must match the name of the class.
3. Constructors must not have a return type.
4. It’s legal (but stupid) to have a method with the same name as the class, but that doesn’t make it a constructor. If you see a return type, it’s a method rather than a constructor. In fact, you could have both a method and a constructor with the same namethe name of the classin the same class, and that’s not a problem for Java. Be careful not to mistake a method for a constructor, be sure to look for a return type.
5. If you don’t type a constructor into your class code, a default constructor will be automatically generated by the compiler.
6. The default constructor is ALWAYS a no-arg constructor.
7. If you want a no-arg constructor and you’ve typed any other constructor(s) into your class code, the compiler won’t provide the no-arg constructor for you. In other words, if you have typed in a constructor with arguments, you would not have a no-arg constructor unless you type it in yourself !
8. Every constructor has, as its first statement, either a call to an overloaded constructor (this()) or a call to the superclass constructor (super()), although remember that this call can be inserted by the compiler.
9. If you do type in a constructor (as opposed to relying on the compiler-generated default constructor), and you do not type in the call to super() or a call to this(), the compiler will insert a no-arg call to super() for you, as the very first statement in the constructor.
10. A call to super() can be either a no-arg call or can include arguments passed to the super constructor.
11. A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you’re free to put in your own noarg constructor.
12. You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
13. Only static variables and methods can be accessed as part of the call to super() or this(). (Example: super(Animal.NAME) is OK, because NAME is declared as a static variable.)
14. Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
15. Interfaces do not have constructors. Interfaces are not part of an object’s inheritance tree.
16. The only way a constructor can be invoked is from within another constructor.

From the above list, point number (7) is the most relevant asnwer for this particular problem set.

 
Paul Clapham
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, "default" is not the most easy-to-understand word. If you think of it as meaning "use this if nothing else is available", that's probably closest to the way it is actually used.
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your understandig

Paul Clapham wrote:Yes, "default" is not the most easy-to-understand word. If you think of it as meaning "use this if nothing else is available", that's probably closest to the way it is actually used.

 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic