Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Acceess Modifiers

 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above Q i thott the right answers are 1 and 2 only.But the given right answers include 3 also.I tried to find out the reason for it and what i concluded is given below.Plz some1 tell me wether i am right or wrong.

It is b'coz the no-args constructor(which is also a default construtor) has default access modifier and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.


regds
Arpana
[ November 25, 2002: Message edited by: Arpana Rai ]
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think so also.
but, why not try to compile it and let jvm tell u why?
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and,
when u extent a from a diff package,
how can u import or refer a class which is in default package?
 
shweta mathur
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is b'coz the no-args constructor(which is also a default construtor) has default access modifier and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.

Arpana, I cudn't understand what u mean to say by "Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.".
The conclusion I could come out after executing ur program and reading through JLS are :
1)An implicit default constructor has the same access modifier as that of the class. As per 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. "
In class A if constructor was not defines at all, it would have been public.
JLS- 8.8.7 Default Constructor
2)An explicit constructor (including the no arg constructor) gets the access according to the access modifier specified in the constructor. If no access modifier is specified as in class A, then the constructor will have default access that is access only in the current package.
3)The option 3 "The class A cannot be extended outside the package in which it is defined. "
in the question is right because
The explicit no arg constructor has default access, and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic