• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A question on constructor

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?

1) A constructor may not be declared as private
2) A Constructor with no arguments will be the default constructor
3) Constructors cannot be overloaded
4) A call to a constructor in a parent class can only be made from within a constructor.
I answered 2 but correct answer is 4!!!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, 2 isn't right because you could declare a no-arg constructor that's private - a default constructor is always coderanch. Number 4 is correct because that's the only place you can invoke the constructor of a parent class.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A1 { A1() {} }
class A2 { A2() { super(); } }
class A3 { A3() { /* lots of code here */ } }
The constructors A1(), A2(), A3() are not the default constructor.
class B {}
The constructor for class B is the default constructor. It looks like B() { super(); }
The constructors for all of the classes do not have parameters.
[ June 12, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
a default constructor is always coderanch.


Correct me if I'm wrong, but I believe the access of the default constructor will match the access of the class. I think I came across a problem where the class was default access, and therefore the default constructor created was also of default access. I wish I could remember what this meant in terms of subclassing, if anything.
Now that I said that, I wonder what this means for inner classes??
[ June 12, 2003: Message edited by: Brian Joseph ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey,
Line3 is calling Base class constructor from a place other than constructor....
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,
If
A2() { super(); }
is not default constructor for class A2
then why
B() { super(); }
is default constructor for class B
Thanks
Barkat
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barkat.
class B does not have any constructor declarations. The compiler provides a default constructor.
class A2 has a constructor declaration.
Here is the definition to remember:
If a class has no constructor declarations, then a *default constructor* that takes no parameters is automatically provided.
The default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The default constructor provided by the compiler for class C is the same code as the explicitly declared constructor for class B.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,

Originally posted by Marlene Miller:
The default constructor takes no parameters and simply invokes the superclass constructor with no arguments.


I don't see anything in the JLS that says that to be a default constructor it must be provided by the compiler. It only mentioned that it will provide a default constructor automatically.
And then, it goes on to define what a default constructor is i.e. it has no argument, simply invokes the superclass w/ no argument, throws no exception, etc. So if you can define a constructor like that manually, then by definition it is a default constructor, isn't it? Then in your example, A2() is a default constructor.
Al
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that option 2 is wrong because


2) A Constructor with no arguments will be the default constructor


lets say we have a class A and the no-arg constructor for A is like

Now in here we have a no-arg constructor, but can it also be called a default constructor, No. And a default constructor matches the class access level but there is no such requirement for a no-arg constructor.
Secondly

4) A call to a constructor in a parent class can only be made from within a constructor.


According to me this is also wrong if a call to a constructor can only be made from a constructor then how would objects be instantiated. Though the opposite of the same is mentioned in the Kathy Sierra and Bert Bates book on Pg 316.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton.
Yes, JLS 8.8.7 says a default constructor is automatically provided. It does not say the compiler provides the constructor.
So I scanned the PDF version of the JLS looking for other places where default constructor is mentioned.

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, this statement should not be qualified by �by the compiler�.

If the class is declared coderanch, then the default constructor is implicitly given the access modifier public

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 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

I propose, but I do not claim, that this is a restatement of 8.8.7.
The statement in 8.8.7 is in the passive voice (is provided) which suppresses the agent doing the providing. This statement is in the active voice (compiler supplies). The agent is the compiler.
Now I move on to The Java Programming Language for insights.

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

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.
It�s also useful to scan the JLS to see how the term �automatic� is used.
Alton, in my opinion, �is automatically provided� means �is provided by the compiler�.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,


Alton, in my opinion, �is automatically provided� means �is provided by the compiler


Yes, I do agree with you on this one. This how I would interpret that statement too. However, the issue is not about that, but the proper definition of a default constructor


class A1 { A1() {} }
class A2 { A2() { super(); } }
class A3 { A3() { /* lots of code here */ } }
The constructors A1(), A2(), A3() are not the default constructor.


From your examples, it is if your saying that to qualify as a default constructor, it must be provided by the compiler. In short, all default constructors must be implicitly declared. You cannot explicitly declare one, like in A1 and A2 classes. This is what I am asking from my earlier post.


I don't see anything in the JLS that says that to be [called] a default constructor it must be provided by the compiler


Now the phrase "automatically provided" in JLS 8.8.7 to me implies that a compiler can automatically provide a default constructor, but you can also manually provide one.
On that same section, it defines the characteristic of a default constructor, such as:
* takes no parameters and simply invokes the superclass constructor with no arguments.
* throws no exception
* etc
IMHO, if you can create a constructor that satisfies all of these, then that is a default constructor. So in your example, A1 and A2 are default constructors.
I'm asking this because assuming that default constructors are only implicitly declared, then faced with questions like this:

The answer would be 'None of the above', because the default constructor will never appear on any source code.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton.
I agree, we need know whether a user-defined constructor A(){super();} is a default constructor, so we can answer the questions correctly.
Notice that �default constructor� in the first sentence of 8.8.7 is in italics. To me that means this sentence is the definition.
If you scan all of chapter 8, the words in italics that are not part of the grammar are all being defined. I cannot find a single counterexample. Sometimes those sentences seem expository. But if you look again, they are definitions.
As I read the Java Programming Language, A(){super();} is called a no-arg constructor.
[ June 13, 2003: Message edited by: Marlene Miller ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate to beat this to death, but there are number of conflicting information that I am getting.
From the book "A Programmer's Guide to Java Certification, by Khalid Azim Mughal, Rolf Rasmussen"


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");
}
//..
}


So it seems from this book, a compiler is NOT the only one that can provide a default constructor.
[ June 14, 2003: Message edited by: Alton Hernandez ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
- a default constructor always has the same access level of the enclosing class. (but not always public)

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton.
Thank you for pointing out the definition in Mughal�s book. Similarly, in Cay Horstmann�s Core Java, a default constructor is a constructor with no parameters. His example is a user-defined constructor with stuff in the constructor body. These two professors use the same definition.
In C++ a default constructor is a constructor that is able to be invoked without user-specified arguments. A default value is associated with each parameter. Complex(double re=0.0,double im=0.0).
Last winter I reviewed Cay Horstmann�s new textbook OOP & Patterns (formerly Mastering OOP with Java). When I pointed out places where his terminology deviated from the JLS, he sometimes changed his words to match the JLS. Other times he did not, because he said he wanted to be consistent with the terminology of the academic world.
Could it be that the professors, influenced by their teaching of C++ and the conventions of the academic world, use a definition of default constructor different from the JLS?
Or could it be that the designers of Java, influenced by C++, allow their default constructor to be user-defined?
[ June 14, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a multi-threaded conversation. There are at least two topics going on concurrently. Let�s clear up the issue on the access modifier of the default constructor. From JLS 8.8.7:

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.

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton.
Mughal�s book is clearly well-written and thorough with good examples and intelligent review questions. However, it is relevant to our conversion to point out the following definitions that vary from the JLS:
Section 2.3, page 29 Variables in Java come in three flavors: Instance variables...Static variables...Local variables (Cp. JLS 4.5.3)
Section 6.2, page 181 A subclass cannot override variable members of the superclass, but it can shadow them. (Cp. JLS 8.3, 6.3.1)
[ June 14, 2003: Message edited by: Marlene Miller ]
reply
    Bookmark Topic Watch Topic
  • New Topic