• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Constructor question

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
while doing a mock exam i found thi squestion :

Q8 Which of the following are valid Constructors?
A1 public Test8(){}
A2 private void Test8(){}
A3 protected Test8(int k){}
A4 Test8(){}


i answered A1,A3 and A4.

solution was only A1 and A3..
my understanding is that hte solutionis wrong... a consructor can have also default modifier.. or, is it?

thanx and regars
marco
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, constuctor can have default access mode.
So A1,A3,A4 are all valid.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is A1 and A3.
A4 is wrong because Test8(){} is same as public Test8(){} and hence cannot be re declared, it will give duplicate constructor error. By default Test8() {} is public.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are wrong.

The affirmation "Test8(){} is same as public Test8(){} " is false because public and default are not the same thing. Default is like "public" only within the same package. Besides that, I think the answers are independents in this case so A4 is not wrong.

Regards,

Leonardo Luiz
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by premierde Sinha:
... By default Test8() {} is public.


You're thinking of the "default constructor," which is automatically supplied by the compiler if no constructor is explicitly defined. In that case, the default constructor has the same access as its class. As detailed in Section 8.8.9 of the JLS:

...if the class is declared public, 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.


So if the class were public, then a default constructor would implicitly be public.

But in this question, it looks like we are talking about a "no-args" constructor explicitly written into the class (not a default supplied by the compiler). An explicit no-args constructor has whatever access is specified, with the usual "default" if none is specified.

Note: This can be demonstrated by creating a package with a public class that has an explicit no-args constructor with no access modifer. If you try to call this constructor from another package (using the correct import), you will get an error because the no-args constructor is not public. On the other hand, if you remove any explicit constructors from the public class and let the compiler supply a default no-args constructor, then the constructor can be called from outside the package.
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more general thing to look for in mock questions is whether the answers are to be taken independently or not. On the real exam, it will always be explicitly stated when answers are NOT to be considered independently. The way this question was stated, if it was on the real exam, you could safely assume that each answer is to be considered independently. On the real exam the question would look something like this:


[ April 12, 2006: Message edited by: Bert Bates ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked the answers on my computer.
A4 is valid.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Spring,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by M Mistroni:
[QB]hello all,
while doing a mock exam i found thi squestion :

Q8 Which of the following are valid Constructors?
A1 public Test8(){}
A2 private void Test8(){}
A3 protected Test8(int k){}
A4 Test8(){}


the answer is A1,A2,A3,A4 all are valid constructors.if the class is public the constructor if it is protected the constructor too, for the default it is default.we also can keep the constructor as private so that it cannot be extended

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sathishmana Kumar:
...the answer is A1,A2,A3,A4 all are valid constructors.


A constructor cannot specify a return type, so A2 is not valid.

Note that this will compile, but it's actually an instance method -- not a constructor...

[ April 13, 2006: Message edited by: marc weber ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic