• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

reference of class!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the following piece of code carefully.

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.
5) The above code will cause a compiler error. The constructors of public class have to be coderanch.
Answer:1,2,3.
can u pl explain what does the option 1) mean?(if possible then with some code)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As A() is default access can not be extended or accessed in a different package
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what it means:


package p1;
public class A(){
A(){}//default constr. with default access
/*public member */
public static void prt(){
System.out.println("A.prt()");
}
}

outside package p1

import p1;
public class SomeClass{
public static void main(String[] arg){
{
//A a1=new A();//cannot instantiate since constructor of A()has default acces
A.prt();//no problem here, you can refer its public members
}
}


THANKS
[This message has been edited by Bindesh Vijayan (edited September 09, 2001).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody tell me why option 3 is correct.
Thanks
--Farooq
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the default access of the constructor doesn't allow to call it outside the package where it was declared
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose,
Does it mean that the public class can be referenced but can not be extended outside its package if the constructor is not coderanch.
--Farooq
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats right because default access has only package accessibility.

Originally posted by Muhammad Farooq:
Thanks Jose,
Does it mean that the public class can be referenced but can not be extended outside its package if the constructor is not coderanch.
--Farooq


reply
    Bookmark Topic Watch Topic
  • New Topic