wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question ID :962546675280 jqplus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question ID :962546675280 jqplus" Watch "Question ID :962546675280 jqplus" New topic

Question ID :962546675280 jqplus

Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
Consider the contents of following two files:
//File A.java
package a;
public class A
A(){ }
public void print(){ System.out.println("A"); }
//File B.java
package b;
import a.*;
public class B extends A
B(){ }
public void print(){ System.out.println("B"); }
public static void main(String[] args)
new B();
What will be printed when you try to compile and run class B?
a) it will print A
b) it will print B
c) it will not compile
the correct ans is c and this is the explanation
Note that there is no modifier for A's constructor. So it has default access. This means only classes in package a can use it. Also note that class B is in a different package and is extending from A. In B's constructor the compiler will automatically add super() as the first line. But as A() is not accessible in B, this code will not compile.
Even though A and B are not in the same package A is public why does the acces modifier to the constructor have to be public i thought since class A is public it is automatically accesible from anywhere
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
The class A is accessible but not its constructor, which means that you cannot create a new instance of A outside of A's package. Also you cannot create a subclass of A outside of A's package because you won't be allowed to access its constructor (the case here).
You know, the constructor could as well be private for that matter even the class A is public... There is no requirements as to how accessible a constructor should be in respect to the class in which it is. That's your job to make up something coherent.

[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Class A is visible everywhere. You can freely refer to A anywhere you want.
You can write
A myVar;
for example, and this will compile.
You can even write myVar.print(); This will compile as well, because the print() method is also accessible everywhere, since it's public.
However as the answer points out, the no-arg constructor is NOT public. In the constructor for B(), since you have not provided an explicit call to a constructor, the compiler does insert a call to the super-class's no-arg constructor, A(). But since the calling method is in a class in another package, A() is not accessible to it. You must either make A() public, OR move A&B into the same package.
One more thing to point out, constructor accessability works just like method accessibility. If you were to make constructor A() private, then no methods outside of class A could access that constructor, even if the accessing class were in the same package.
[ March 08, 2002: Message edited by: Rob Ross ]

SCJP 1.4
Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
thanks guys i assumed it as long as the class was public it was accessible anywhere i did not think i had to take the access modifier of the constructor in consideration the more i study the more i feel am not getting anywhere with this exam
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
make the difference between class accessibility and class members accessibility. These are two different things. A class may be accessible while its members not. If a class is not accessible so are its members, that is, if you cannot access a class you sure cannot access its members (even the latter are public!)
Thinf of the class as a house. If you know where is the house, you can get there (class is accessible). Now if the house is opened, then you can get in and use its facilities (members). Maybe some rooms will be closed (members are not public) and you won't be able to get in those.
If you don't even know where the house is (class not accessible), then you really can get in to use its facilities (members) even if all rooms are opened..
I hope that clear things up.
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
I felt exactly the same way while studying for the exam. The more you learn, the more you run into troubles you never thought existed...
But I would say that this is a sign of growth. You might not be ready yet, but it is only a matter of time before you are. Dive into the books, write lots of codelets and ask lots of questions here and there, and you'll be fine!
But this is just me; I like to see things positively...!
Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
thanks Kasper for the encouragement .thanks Valentin for clearing things up i got it the first time i was just thinking out loud about stuff i would never have considered if not for this exam
[ March 08, 2002: Message edited by: Tosin Adedoyin ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
For what it's worth:

Good Luck,
-Dirk Schreckmann

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link: http://aspose.com/file-tools
subject: Question ID :962546675280 jqplus
It's not a secret anymore!