This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output of the Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Output of the Program" Watch "Output of the Program" New topic
Author

Output of the Program

Kalai Selvi
Ranch Hand

Joined: Jan 08, 2007
Posts: 45
The Piece of code goes like this:

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
the answer should be 4.
since class A is Public, JVM will implicitly assign public access modifier to constructor of A.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Isn't the answer 2 since having no access qualifier is the default which is package access?

Kaydell
Tannis Bo
Greenhorn

Joined: Aug 08, 2006
Posts: 6
I would've gone with 4 as well...
Cao Bin
Greenhorn

Joined: Apr 23, 2007
Posts: 14
I have just made a test.

when compile,the compile error like this:
A() in a.A is not public.so can't be get from other packages.

so 2,3 should be choosed


i love you not because who you are but because who i am when i'm with you
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
It can not be instantiated as well as extended outside the package as constructor of A is not visible over there.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
My choice is 4
Since class is public constructor should be public. right?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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
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
Hi anil

You are right. Thank you.
Then what is the answer here
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi,

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)

HtH.
[ June 12, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
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
Hi

suresh


The answers are 2 and 3


Thanks

Anil Kumar
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
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
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
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

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
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.




Thanks

Anil Kumar
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I think what Anil said is right.

Ishita Saha,

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
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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Krishna,

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
Hi,

a small sample that demonstrates that option 1 is correct.



Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Manfred,

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
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
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
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


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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


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)

Good luck!
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
My choices are 2 and 3.


SCBCD, SCJP & MCP
HowToAskQuestions
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
HI

Answer must be 4
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285

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 ]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
yes Sergio Tridente! you are correct .Thanks
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185

1.The class A can be referenced outside the package in which it is defined.


yes, it seems the above statement is right.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Output of the Program
 
Similar Threads
class access
constructor
Doubt
Access modifiers question
reference of class!