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

Question ID :962546675280 jqplus

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tosin,
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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!
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...!
/Kaspar
 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
For what it's worth:



Good Luck,
-Dirk Schreckmann
 
Do Re Mi Fa So La Tiny Ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic