• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Field

 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that a private field of a superclass might be accessible to a subclass (for example, if both classes are members of the same class).

This is a quote from JLS(8.3).
Do you know what this mean? Can you give an example?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How's this for an example:

Notice that the subclass can access the private member of the superclass because the subclass is also an inner class of the superclass.
I'm not really sure when you'd ever need to do something like this, but, as with most things you run into with the SCJP cert, you need to know what can be done, not necessarily what should be done.
I hope that helps,
Corey
[ June 06, 2003: Message edited by: Corey McGlone ]
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a private field of a superclass might be accessible to a subclass (for example, if both classes are members of the same class).
My be it means something like this ....
public class test
{
class t1
{
private int i=100;
}
class t2 extends t1
{
public void m()
{
t1 ob=new t1();
System.out.println(ob.i);
}
}
}
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,

Notice that the subclass can access the private member of the superclass because the subclass is also an inner class of the superclass.

Thanks for the reply.
But I still have a question:
Is the outer class considered the superclass of the inner class?
In that phrase(JLS 8.3), I was thinking along the line of 'extended' classes, because the paragraph before that, it was talking about inheritance.
Al
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey
I think that the example you have given doesn't involes a subclass. I am posting a code that shows the subclass accessing a private member of the superclass.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praful,
I think that's the right example
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is the outer class considered the superclass of the inner class?

Sorry about that, I modified the code above. Apparently, when I was copying my code for the reply, I forgot to attach the "extends Test" clause. Regardless, the code produces the same output. Sorry about that.
Corey
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Since x is private, B does not inherit x.
However, x is accessible to B.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A practical example might be where the inner classes are declared public, something like this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic