• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

public

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic