Originally posted by Corey McGlone:
a default constructor is always coderanch.
Originally posted by Marlene Miller:
The default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
2) A Constructor with no arguments will be the default constructor
4) A call to a constructor in a parent class can only be made from within a constructor.
If some default constructors were not provided by the compiler, this statement should not be qualified by �by the compiler�.A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have an accessible constructor that takes no arguments. (8.8.7)
If some default constructors were not provided by the compiler and the provider declared the class public and the default constructor private, this statement implies the private access is superceded by the implicit public access.If the class is declared coderanch, then the default constructor is implicitly given the access modifier public
I propose, but I do not claim, that this is a restatement of 8.8.7.If the source code for a class contains no declared constructors, the Java compiler automatically supplies a constructor with no parameters. Adding one or more constructor declarations to the source code of such a class will prevent this default constructor from being supplied automatically, effectively deleting a constructor, unless one of the new constructors also has no parameters, thus replacing the default constructor. The automatically supplied constructor with no parameters is given the same access modifier as the class of its declaration, so any replacement should have as much or more access if compatibility with pre-existing binaries is to be preserved. 13.4.11
Notice �the language provides� in this statement. In truth, I have always *assumed* the compiler provides the default constructor, because I learned about it from this book.Constructors without arguments are so common that there is a term for them: they are called no-arg (for �no arguments�) constructors. If you don�t provide any constructors of any kind in a class, the language provides a default no--arg constructor that does nothing. This constructor�called the default constructor--is provided automatically only if no other constructors exist because there are classes for which a no-arg constructor would be incorrect. 2.5.1
Alton, in my opinion, �is automatically provided� means �is provided by the compiler
class A1 { A1() {} }
class A2 { A2() { super(); } }
class A3 { A3() { /* lots of code here */ } }
The constructors A1(), A2(), A3() are not the default constructor.
I don't see anything in the JLS that says that to be [called] a default constructor it must be provided by the compiler
pp. 101
A default constructor is a constructor without any parameters. If a class does not specify any constructors, then an implicit default constructor is supplied for the class.
pp. 102
A class can choose to provide an implementation of the default constructor. In the example below, the class Light provides an explicit default constructor at (1).
class {
//...
// Explicit Default Constructor
Light() { //(1)
noOfWatts = 50;
indicator = true;
location = new String("X");
}
//..
}
Originally posted by Corey McGlone:
- a default constructor always has the same access level of the enclosing class. (but not always public)
Regards,<br />Meghasyam.<p>SCJP1.4
If the class is declared coderanch, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.
The rule that the default constructor of a class has the same access modifier as the class itself is simple and intuitive.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |