This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor related

 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found a statement in Dan's mock exam:
f. The implicitly declared constructor has package access.
the answer is "false"
can any one help me in understanding it?
regards and thanks
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to every class that does not declare a constructor, one with no arguments will be provided!!
example:
public class A{
}
you are free to create instances of A using new A(), which is an implicit declared constructor. But with class B:
public class B{
public B(int x){}
}
you can only create instances of B using the only the following statement: B b=new B(i); for example. There is no empty constructor. So u cannot create a b instance with "new B()".
This implicit constructor has the same access of
the owner class.
public class A{}
is the same as
public class A{
public A(){}
}
BUT
class B{}
is the same as
class B{
B(){}
}

notice that the access of class B is the same of the implicit constructor. In this case, package level access!
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the full text of the question.


Which of the following statements are true?
a. class A extends Object.
b. Field i1 is implicitly public because class A is public.
c. Method m1 is implicitly public because class A is public.
d. The compiler will insert a default constructor implicitly.
e. The implicit constructor declaration has no throws clause.
f. The implicitly declared constructor has package access.
g. The implicitly declared constructor accepts one parameter for each field in class A.
h. The implicitly declared constructor invokes the no-parameter constructor of the superclass.
i. None of the Above
As Leandro has stated, the implicitly declared constructor has the same access level as the class. In this case, the access level of the class is public so the access level of the implicitly declared constructor is also public. Therefore, answer option f is false.
The above is a demonstration of why an answer option quoted out of context can be misleading. When asking for an explanation of an answer it is always a good idea to also quote the question.
 
Tausif Khanooni
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both.
sorry dan, i will remember your point hereafter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic