• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Output of the Program

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer should be 4.
since class A is Public, JVM will implicitly assign public access modifier to constructor of A.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't the answer 2 since having no access qualifier is the default which is package access?

Kaydell
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would've gone with 4 as well...
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can not be instantiated as well as extended outside the package as constructor of A is not visible over there.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My choice is 4
Since class is public constructor should be public. right?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the class is public, it can be referenced outside of the package.

So 1, 2 and 3 are correct.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anil

You are right. Thank you.
Then what is the answer here
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

suresh


The answers are 2 and 3


Thanks

Anil Kumar
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

a small sample that demonstrates that option 1 is correct.



 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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!
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My choices are 2 and 3.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI

Answer must be 4
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
raj malhotra
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Sergio Tridente! you are correct .Thanks
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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



yes, it seems the above statement is right.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic