*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public" Watch "public" New topic
Author

public

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
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.
were the given answers. Why 2 and 3 are true? Anyidea?
- Thanks
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Idea: because the constructor is not declared as public, it is declared with default (zero) access modifier = package visibility. But I am not sure, I also would choose only 1 as right answer...


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
yanish
Greenhorn

Joined: Aug 18, 2000
Posts: 21
Mapraputa's idea is completely correct. A main point here is that a default constructor has been defined with package accessibility. So, there are no any ways to instantiate the class (or extend the class) outside the package where it is defined.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am not clear !!! Why is (1) wrong? Since the class is public we can a refernce of this class outside. For eg:
package OtherPackage;
public class A
{
A(){}
}
public class B extends A
{
public B(){}
public A returnReference()
{
return new A();
}
}
Now consider this:
package P;
class Test
{
OtherPackage.A a = new B().returnReference();
}
So, out here u are able to reference class A outside the package. So, how can we say that the class A can be referenced outside the package in which it is defined?
Shouldn't (1) be true as well??
Please correct me if I am wrong!!
- sampaths77
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: public