• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class accessibility

 
Greenhorn
Posts: 28
  • 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.
4. The class A can be referenced, instantiated or extended anywhere.
What is the meaning of "class A can be referenced " in "1".
The answer is 1, 2 & 3.
Can anyone explain,
Giving the exam tomorrow
Thanks
kanchan
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reference:

A referenceToA = null;

Instantiation:

referenceToA = new A();

You can create a reference to an object of type A without actually creating an A object.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you get this question? Just curious...
 
kanchan chaudhary
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is somewhere on this site:http://www.angelfire.com/or/abhilash/Main.html
It tests the JLS concepts. A very good site.
Anybody can make the concept more clear.
Thanks
kanchan
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanchan,
When a class is declared as 'public' it's name can be seen by any other class. For example, the following will compile without error if it is in the same directory as 'A':

Because class 'A' was declared to be public, any class can create a reference variable of the class 'A' type.
'TestA' can call the 'A' ctor as they are both in the same directory and do not have package names.
However, the following code, stored in a seperate package, fails:

You can still create a reference variable of type 'A'; since the class itself is 'public' but you can no longer create an object of type 'A' because the ctor had no access modifier and therefore defaults to 'same package' access.
Attempting to extend the 'A' class from another package also fails due to the 'package only' access of the ctor.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
kanchan chaudhary
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
public class A
{
A()
{
}
}
In the aboce class, the constructor has public accessibility. If the constructor is not given any modifier, then it defaults to the accessibility of the class, so in the above example, since the class is public, the constructor is also public, unless explicitly mentioned private/protected.
Pls correct me if I am wrong.
Thanks
kanchan
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kanchan---all the best for your exam on 8th! Do well.......
Aditi Sharma
Sun Certified Programmer for Java2 Platform
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by kanchan chaudhary:
> Pls correct me if I am wrong.
What you described is true of implicit or default constructors provided by Java when no explicit constructors are provided. For explicit constructors, the visibility is what is specified. In the given code, the constructor A() has default/package/friendly visibility.
Junilu
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanchan,
Default modifier means that it is visible within the same
package and it is not "PUBLIC" modifier as described by you.
Thus the answer 1, 2, 3 are the correct answers.
Ravindra Mohan.
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic