Results 1 to 3 of 3
  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Rep Power

    Default A class cannnot see it's own protected variable from another package

    I'm doing some tests to get a concrete understanding of how access levels work from subclasses and other packages, and I have discovered that a class cannot see it's own protected variable from another package which I thought it would be. I know that it says in the java docs "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." but I thought that would also include it's own class. Does anyone know the reason why this is? I'm just curious :)

    Java Code:
    package food;
    import food.fruit.*;
    public class Food {
        protected int protecte = 5;
        private int privat = 5;
        protected void method(){
    Java Code:
    package food.fruit;
    import food.*;
    public class Apple extends Food {
        public void eat(Food food){
            System.out.println(this.protecte); // WORKS: Subclass in another package can see protected variables from it's superclass.
            System.out.println(food.protecte); // ERROR: protece has protected access in Food.
            food.method(); // ERROR: method() has protected access in Food.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Rep Power

    Default Re: A class cannnot see it's own protected variable from another package

    Because when you set 'food' as a parameter, you are making a new instance of the food class, that means you are trying to access the protected variable by instance, no longer subclass. This will not work.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: A class cannnot see it's own protected variable from another package

    Put your Apple class in the same package as Food. Then it will work. If they are in different packages, override method() in the apple class like this:

    Java Code:
    public void method() {
    Then you can effectively call the protected method of the super class, even if the Apple class is in a different package.

    Last edited by jim829; 01-31-2014 at 01:16 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 01-15-2014, 08:27 AM
  2. Class can't see public variable in the same package
    By gogonimago in forum New To Java
    Replies: 4
    Last Post: 07-06-2012, 10:00 PM
  3. Replies: 3
    Last Post: 03-14-2009, 08:22 AM
  4. Replies: 1
    Last Post: 08-07-2007, 07:53 AM
  5. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts