Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Package Doubt :(

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic