• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

About packages and complications - total confusion

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Bill and everyone
i think i understand it now
shailendra
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic