Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please answer the question and explain it to me,thanks!

 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all!
I got it!
 
Paulo Silveira
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this should work!!!
why does it not work?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic