• Post Reply Bookmark Topic Watch Topic
  • New Topic

class access  RSS feed

 
sonali rao
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the following code and choose the correct options
public class A
{
A()
{
}
}
1.The class A can be referenced outside the package in which it is defined.
2.The class A cannot be instantiated outside the package in which it is defined.
3.The class A cannot be extended outside the package in which it is defined.
4.The class A can be referenced, instantiated or extended anywhere.
Answer given:3.
I think the answer should be 2 and 3.
how will the class be instantiated outside the package if it cannot be extended outside the package?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must've deleted my message.
You're correct, class A cannot be instantiated or extended outside of its package because the constructor is marked default. However, if you had public static fields defined in A, you'd be able to use them outside of the package.
 
sonali rao
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.
I have something to clarify. you said public static fields are accessible. Is it because the class access modifier is public? did i get this right?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sonali rao:
Thanks for the quick reply.
I have something to clarify. you said public static fields are accessible. Is it because the class access modifier is public? did i get this right?

Yes, if the class is declared as public and properly imported, you can use its static public fields and methods outside of the package. Even if its constructor is private (Math class is a good example of that).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!