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.
m Sabale
Greenhorn
Joined: Mar 22, 2005
Posts: 5
posted
0
the answer should be 4. since class A is Public, JVM will implicitly assign public access modifier to constructor of A.
My choice is 4 Since class is public constructor should be public. right?
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Since the class is public, it can be referenced outside of the package.
So 1, 2 and 3 are correct.
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
posted
0
Hi
suresh
NO.
Because it is not provided by the compiler.It is provided by the programmer.If it is provided by the compiler then the accessmodifier it will get is the modifier specified in the class.
Thanks
Anil Kumar
Ankith suresh
Ranch Hand
Joined: Jun 07, 2007
Posts: 42
posted
0
Hi anil
You are right. Thank you. Then what is the answer here
Ankith suresh
Ranch Hand
Joined: Jun 07, 2007
Posts: 42
posted
0
package pk1; public class SCJP11 { SCJP11() { } } ---------------------------------
package pk2; import pk1.SCJP11; class SCJP11Test extends SCJP11 //1 {
}
When i compile the SCJP11Test.java i am getting following error
SCJP11Test.java:3: SCJP11() is not public in pk1.SCJP11; cannot be accessed from outside package class SCJP11Test extends SCJP11 ^ 1 error
Why the error is displaying while extending..? Compiler trying to access the constructor while extending the class? I thought error will display only when trying to create the object.
I have also tested now. I use JDK 1.4.2_12 (fyi and if reqd). I have not got any exceptions or errors. The code compiled fine.
Lets look at the options one by one....
option 1: It holds good as the access modifier of class is given as public. So its valid and can be chosen.
option 2: It holds good because the class is of default package and the constructor is not public!
option 3: I think this will be ruled out! because, extension does check the class's access level and the class is of public access, it should be allowed to be extended.
option 4: It is not applicable because of action 2 is ruled out (instantation).
option 5: It does not hold good as there are no hard and fast rules of the public class to have the constructor as public. It can have non-public constructors as well. (thats why the above program compiled fine without any exceptions)
4.The class A can be referenced, instantiated or extended anywhere. see the code below:
According to above code, the option 4 is ruled out. The answere should be 2,3.
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
posted
0
Hi
suresh
The answers are 2 and 3
Thanks
Anil Kumar
Ishita Saha
Ranch Hand
Joined: May 30, 2007
Posts: 39
posted
0
Hi,
I tried creating a reference of class A in another class B which is declared in different package.
So i just said A a1; and it works. the compiler complains only when i say:
a1 = new A(); so going by that 1,2,3 should be the answer.
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
posted
0
Hi
Saha -------------------------------------------------------------- So i just said A a1; ----------------------------------------------------------------
Ya what you said is true.But here a1 is not refering to any thing.You just declare a1 of type A.If it tries to refer to A or any of its subclass ,you will get error.
Thanks
Anil Kumar
Ishita Saha
Ranch Hand
Joined: May 30, 2007
Posts: 39
posted
0
but then what would be the difference between instantiating A and creating a reference to A i.e option 1 & 2
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
posted
0
but then what would be the difference between instantiating A and creating a reference to A i.e option 1 & 2
Instantiating means creating the object. I am instatiating the class A means, i am making an object of class A. A a = new A(); I am creating a reference of A means, simple declaring a variable as type A. A a; Here a is the identifier of type A. you can assign an object of type A to a or the object of its child class. Hope you got the point.
anil kumar
Ranch Hand
Joined: Feb 23, 2007
Posts: 447
posted
0
Hi -------------------------------------------------------------------------- 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. --------------------------------------------------------------------------- 1) false
Here A can be referenced, means we can assign the object of type A or sub type of A,which we can't do because the sub clsss can't find the constructor of the super class. 2)true means A a1=new A();
we can't do this because A is in the other pacakge.
Hope krishna answered your question. To summarize,
Krishna,
I think the sample code you had given to explain is having both the classes A and B in one package. But in the question given by OP does not mention about anything and its an abstract term when given in the options.
HtH.
krishna bulusu
Ranch Hand
Joined: Aug 28, 2006
Posts: 185
posted
0
Anywhere means every place like same package, different package etc etc. but, we can do it in the same package level. so, every where is ruled out. it should be some where means to a particular area.
I think the term "anywhere" in the question is quite subjective and with respect to this scenario, i tend to interpret it as "everywhere" only. Means, in all the places without any exception.
Am i right? Or is it the other way around as you said?
Ranchers, please clarify!
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi,
a small sample that demonstrates that option 1 is correct.
Thats fine with your code. But the OP has posted only with the constructor with default visibility. Thats where the question started off.
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by Raghavan Muthu:
But the OP has posted only with the constructor with default visibility. Thats where the question started off.
Even then can A be referenced outside the package.
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
posted
0
Just to be sure, that Ankith suresh's post is not being overlooked:
Option 3 is false. See post at 9:29 and to answer your question Ankith: If you don't give a constructor explicitly in the derived class, the compiler adds a default-constructor including a call to super() and this called constructor is not visible, so of course the compiler complains
1: yes 2: yes 3: no 4: no 5: no
[Edit]
And as there seems to be confusion around option 1, just see this code of three files:
A is referenced in another package.
[Edit 2]
Sorry Manfred, you already made it clear [ June 12, 2007: Message edited by: Sasha Ruehmkorf ]
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Hi Sasha,
a small correction. Option 3 is true.
3.The class A cannot be extended outside the package in which it is defined.
[ June 13, 2007: Message edited by: Manfred Klug ]
Sasha Ruehmkorf
Ranch Hand
Joined: Mar 29, 2007
Posts: 115
posted
0
I'll argue the converse. How comes, I was sure that point 3 stated that it is possible to extend from outside the package.
My certification exam starts in 2 hours and it seems like I have to read the questions and answers more carefully
it seems like I have to read the questions and answers more carefully
Yes, thats very much true. I remember one of the ranchers was having a quote in his signature as, "We should know the questions properly before the answers!"(the sentence may not be appropriate)
1.The class A can be referenced outside the package in which it is defined.
class A can be referenced outside the package if we import the class in other package and its constructor is public. which is not the case so this is not the answer.
2.The class A cannot be instantiated outside the package in which it is defined.
Even if we import the class A,we cannot instantiate there as constructor is not visible.so This answer is ok
3.The class A cannot be extended outside the package in which it is defined.
class A cannot be extended because subclass constructor need to call super constructor which is not visible so class A cannot be extended outside the package.so this is also correct.
4.The class A can be referenced, instantiated or extended anywhere.
only in the same package.so this is not correct.
5,The above code will cause a compiler error. The constructors of public class have to be public.
we can specify any access modifier for a top level class.so this is also not correct [ June 14, 2007: Message edited by: raj malhotra ]
Originally posted by raj malhotra: class A can be referenced outside the package if we import the class in other package and its constructor is public. which is not the case so this is not the answer.
Option one is OK. Class A can be referenced outside package, it does not matter if it cannot be intantiated. Take for instance
You can refer to A in second package, but you still cannot create an instance of A, so we used a creator class that 'lives' in the first package. [ June 14, 2007: Message edited by: Sergio Tridente ]