File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Package Doubt  :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Doubt  :(" Watch "Package Doubt  :(" New topic
Author

Package Doubt :(

Satish Kumar
Greenhorn

Joined: Feb 18, 2005
Posts: 23
Here is my code :

FILE NAME : A.java
-------------------------------------------------------------
package p1;
public class A
{
int x;
public int y;
protected int z;
private int k;


void def()
{
System.out.println("DEFUALT fun");
}
public void pub()
{
System.out.println("PUBLIC fun");
}
private void pri()
{
System.out.println("PRIVATE fun");
}
protected void prot()
{
System.out.println("PROTECTED FUN");
}
}
-------------------------------------------------------------------------------

CASE 1 : wen i try to ACCES the protected memebers and functions of p1.A thru the packdemo's reference as following i dont get any error.
------------------------------------------------------------------------------- --
package p2;

public class pathdemo extends p1.A
{
public static void main(String[] args)
{
pathdemo a=new pathdemo();
System.out.println(a.y);
System.out.println(a.z);
a.pub();
a.prot();

}
}
-------------------------------------------------------------------------
CASE 2 : but wen i access the p1.A class ,protected memebers thru p1.A's reference outside the package p1 i get the error(shown beow the code)
-----------------------------------------------------------------------
package p2;

public class pathdemo extends p1.A
{

public static void main(String[] args)
{
p1.A a= new pathdemo();
System.out.println(a.y);
System.out.println(a.z);
a.pub();
a.prot();

}
}
ERROR :
pathdemo.java:13: z has protected access in p1.A
System.out.println(a.z);
^
pathdemo.java:15: prot() has protected access in p1.A
a.prot();
^
2 errors
------------------------------------------------------------
I am very new to JAVA 's packages , can any 1 plzz explain the reason behind this error .
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
A protected variable can only be accessed from another class if it's in the same package. You have on class in package A and the other in package B. The member variable that you are trying to access has a protected access modifier. So you can not address it unless you make it public. (Or change the packages).
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Maybe I should add to this!

The protected methods/variables are also available to all classes that extend from the class that has those protected items. No mather if they are in the same package.

Access Modifier tutorial
[ June 21, 2005: Message edited by: Manuel Moons ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Manuel's second post still doesn't explain Satish's observation. Perhaps surprisingly, extending a class does not give you access to protected members of instances of the superclass -- the rule is that you only have that access on instances of your subclass, or sub-subclasses of that subclass. This explains why the first example works, but the second does not.

Likewise, if two classes B and C extend a class A, code in B cannot access A's protected members on instances of A or C, but only on instances of B.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Doubt :(