aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Field " Watch "Private Field " New topic
Author

Private Field

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443

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

Joined: Dec 20, 2001
Posts: 3271
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 ]

SCJP Tipline, etc.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 641
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);
}
}
}


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: May 30, 2003
Posts: 443
Hi Praful,
I think that's the right example
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

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

Joined: Mar 05, 2003
Posts: 1391

Since x is private, B does not inherit x.
However, x is accessible to B.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
A practical example might be where the inner classes are declared public, something like this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private Field