| Author |
please answer the question and explain it to me,thanks!
|
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
|
|
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 ?
|
help you means help me
|
 |
Jamal Hasanov
Ranch Hand
Joined: Jan 08, 2002
Posts: 411
|
|
You cannot access protected variable of A inside B. Therefore your code will not compile. ===============================================
6.6.2 Details on protected Access A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
=============================================== It will compile: 1) If you'll change code like this package p2; import p1.*; public class B extends A{ public void process(B b){//new b.i = b.i*2; } public static void main(String[] args){ A a = new B(); B b = new B(); b.process((B)a);//new System.out.println( a.getI() ); } } 1) or change package package p1; import p1.*; public class B extends A{ ... } Jamal Hasanov www.j-think.com
|
 |
Nandkishore Dhilde
Greenhorn
Joined: May 09, 2002
Posts: 20
|
|
When try to compile Class B then gives error at public void process(A a) { a.i = a.i*2; } b'coz i is protected variable of p1 it can'nt accessed in another packages.
|
SCJP1.4 certified, IBM SOA Solution Designer[2007]
|
 |
Jamal Hasanov
Ranch Hand
Joined: Jan 08, 2002
Posts: 411
|
|
No Nandkishore, Protected members can be accessed from other packeges - Members with default access modifier cannot be accessed from other packages. Jamal Hasanov www.j-think.com
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
Originally posted by Jamal Hasanov: Protected members can be accessed from other packeges - Members with default access modifier cannot be accessed from other packages.
Be careful with what you say Jamal --> Protected members can only be accessed by subclasses from other packages.
here's a quick list from A Programmer's Guide to Java Certification: public - Accessible everywhere. protected - Accessible by any class in the same package as its class, and accessible only by subclasses of its class in other packages. default(no modifier) - Only accessible by classes, including subclasses, in the same package as its class (package accessibility). private - Only accessible in its own class and not anywhere else.
Also, Seany -- you can easily figure this one out for yourself simply by putting the code into the appropriate files / folders and try compiling it yourself, you get the following error message -- which explains what the problem is:
|
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
|
 |
Jamal Hasanov
Ranch Hand
Joined: Jan 08, 2002
Posts: 411
|
|
OK, Jessica There was a typo, must read as : ------------------------------------------ Protected members can only be accessed by subclasses from other packages. ------------------------------------------ I hope you're agree with my explanation for this code behaviour. Regards, Jamal Hasanov www.j-think.com
|
 |
Francisco A Guimaraes
Ranch Hand
Joined: Mar 20, 2002
Posts: 182
|
|
Hi, i noticed something strange: if you change "p1.A" for A in the first example, like this: ... public class B extends A //<--was p1.A ... the program compiles, runs and prints 20. what�s the difference between extending "A" and "p1.A"? Thanks, Francisco
|
Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Originally posted by Francisco A Guimaraes: what�s the difference between extending "A" and "p1.A"?
As far as I know, there should be no difference. I know the current compilers seem to have some problems when it comes to access of protected members, so I'd chalk this one up to a compiler bug. Anyone else have any ideas? Corey
|
SCJP Tipline, etc.
|
 |
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
|
|
thanks all! I got it!
|
 |
Paulo Silveira
Ranch Hand
Joined: May 21, 2002
Posts: 32
|
|
this should work!!! why does it not work?
|
 |
 |
|
|
subject: please answer the question and explain it to me,thanks!
|
|
|