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
Joined: Jul 03, 2001
Posts: 2120
posted
0
As A() is default access can not be extended or accessed in a different package
SCJP2. Please Indent your code using UBB Code
Bindesh Vijayan
Ranch Hand
Joined: Aug 21, 2001
Posts: 104
posted
0
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
Joined: May 08, 2001
Posts: 356
posted
0
Can somebody tell me why option 3 is correct. Thanks --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
Because the default access of the constructor doesn't allow to call it outside the package where it was declared
Muhammad Farooq
Ranch Hand
Joined: May 08, 2001
Posts: 356
posted
0
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
Joined: Nov 03, 2000
Posts: 267
posted
0
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