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

public

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
yanish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic