File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About packages and complications - total confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About packages and complications - total confusion" Watch "About packages and complications - total confusion" New topic
Author

About packages and complications - total confusion

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
A question from JQ+
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 ?
Answers : Will print 10; 20 ; not compile ; runtime excep
Pls spare time to explain concept behind it --- THANX
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
This in my opinion is a tough one. I got this wrong the first time through.
The answer is that it won't compile.
Reason:
While "i" is protected in Class A, so you can access in the same package or with a subclass of A, you would think that you can access it in class B just fine. Well you can, but in this example, they are not accessing it in class B directly, they are using a reference of class A to get to "i". This would work if they were in the same package, but since they are not, the reference variable "a", can't see "i".
Confusing, and I don't know if I explained it well. So maybe someone can try to explain it better.
Bill
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
i tried many interesting variations on this code and so should you.
well, what do i explain... your questions should be more specific.
1)there is runtime polymorphism only regd. over-ridden methods.
2)inside a method a call to another variable and method is supercededed by the implicit this.
3) blah blah blah...
i suggest you copy the method getI() into B too, and then see the result.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
That's true , it's only available inside it's own package and to sublcasses , I believe that you can get access to i though the reference of B but not though A.


Val SCJP <BR>going for SCJD
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
uh oh! this is a package problem... i just saved time by putting it all in one package! sorry....
here's the answer....
if c extends b and b extends a, and a has a protected variable i, and they are in different packages-
b can call its own i.
b can call its sub-class' c's i,
but it can't call a's i.
Lee Clarke
Greenhorn

Joined: Jan 18, 2001
Posts: 18
Ah the answer to that through me off for a bit and I had to go try this out.
I discovered that while i is protected, b cant access i located in an A object but if you pass a B into a method of A's A can access b.i
such as this:

for some reason I kept thinking that since it is protected that a subclass could access a parent objects protected member, but really the subclass can contain an instance of a protected member but it cant access a parents parents protected member.
Did I state that correctly?

[This message has been edited by Lee Clarke (edited February 02, 2001).]
[This message has been edited by Lee Clarke (edited February 02, 2001).]


Lee Clarke - SCJP
Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
Thanx Bill and everyone
i think i understand it now
shailendra
Ananda Kashyap
Greenhorn

Joined: Jan 03, 2001
Posts: 23
Hi,
I have modified B.java file and want to access only a.i it gives me compiler error saying "I can't access the protected i". Is it because, protected variables can't be accesed by instantiation?
Ananda.

package p2;
import p1.*;
public class B extends p1.A {
public static void main(String[] args) {
A a = new A();
System.out.println( a.i); // Compiler error
System.out.println( getI()); // OK, returns 10
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About packages and complications - total confusion
 
Similar Threads
Compile or not?
Question from JQPlus
why compile time error???
How does this work ?
please answer the question and explain it to me,thanks!