• 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

need help from JQ+

 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just gave a Test exam of JQ+ and found this queestion.
Question ID :957672686580
Consider following two classes:
//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
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();
b.process(a);
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....

------------------
azaman
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashik
as u might hav tried as well.. the code wont compile ..and it shld not
the comipler compalins this way ;

B.java:19: i has protected access in p1.A
a.i = a.i*2;
^
B.java:19: 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;
10
20

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).]
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic