File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help from JQ+ 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 "need help from JQ+" Watch "need help from JQ+" New topic

need help from JQ+

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

I just gave a Test exam of JQ+ and found this queestion.
Question ID :957672686580
Consider following two classes:
//in file
package p1;
public class A
protected int i = 10;
public int getI() { return i; }
//in file
package p2;
import p1.*;
public class B extends p1.A
public void process(A a)
a.i = a.i*2;
public static void main(String[] args)
A a = new B();
B b = new B();
System.out.println( a.getI() );
What will be the output of compiling and running class B ?
I thought it will print but it does not compile. JQ+ is really a good exam simulator! I have read the answer but need further clarifications....


Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
as u might hav tried as well.. the code wont compile ..and it shld not
the comipler compalins this way ; i has protected access in p1.A
a.i = a.i*2;
^ i has protected access in p1.A
a.i = a.i*2;
2 errors

well this is exaclty wht is expected from this code
following r the ways to access a member wid protected access modifier ;
- Class itself can acess 'protected' member ( of course)
- Subclass( or other classes) in the same package can access the 'protected' memeber as well ( obvious enough )
- Subclass in another package can access 'protected' memeber , But there is a string-attached !
the catch is tht Subclass in another package should access protected-memeber either through its own reference
( like, expicitly using keyword this or tht can be implicit too )
or using reference of it subclass . No other way it shld try to access protected members
( and wht could be other ways?? just wht our problematic code tries to do is a OTHER-way ,
subclass-in-another-package canNOT use the reference of class itself, or its super class ( or super's super-clas etc )to access protected-member)

i quote a line from khalid's 4th chapter

...a subclass in another package can only access protected memebers in the superclass via references of its own type or a subclass...

well.. if we change the code a lill bit , then it goes-well with the access rules for protected modifier

this code compiles n run wid output;

remember , protected modifier 'protects' access through a proper-reference type
for subclasses in diff package , refernce-type need to be out of these two, subclass-in another package or its subclasses
for more on this i refer u to 4th chapter of khalid ( page 117)
or sec 6.6.2 of JLS 2nd ed ( a similar example is 6.6.7 in JLS , must see)
and after going through all the refrences , n understanding the concept ,
give a look to following code ( i changed the above code ) , n if u hav got wht
protected is all about , this code shld be breeze for u

have chilling preparation ! n good luck !

[This message has been edited by Gagan Indus (edited August 20, 2001).]

Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="" target="_blank" rel="nofollow">My Blog</a>
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Thnx Gagan!
Though a little bit comlex example (code) , i actually knew the theory but realized it after getting ur strings from JLS. This is really Java.....
Muhammad Ashikuzzaman (Fahim)
When you learn something, learn it by heart!
I agree. Here's the link:
subject: need help from JQ+
It's not a secret anymore!