File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reference of class!

 
ashok khetan
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • 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 public.
Answer:1,2,3.
can u pl explain what does the option 1) mean?(if possible then with some code)
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As A() is default access can not be extended or accessed in a different package
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody tell me why option 3 is correct.
Thanks
--Farooq
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 public.
--Farooq
 
Roopa Bagur
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • 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 public.
--Farooq

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic