Protected variable from superclass invisible from subclass in other packaging

I have two classes in different packages as follows. This is the base class in the package library.

package library;

public class Book{

    public int varPublic;
    protected int varProtected;
    private int varPrivate;
    int varDefault;
}

and this is a subclass in package building.

package building;

import library.Book;

public class StoryBook extends Book {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Book book = new Book();
        book.varPublic = 10;
        book.varProtected = 11;


    }

}

I understand that the variable "var.Protected" should be visible in the StoryBook class, but I get an error message. I tried to execute this code from eclipse and command line.

can anyone look at this

+2
source share
1 answer

Classes in other packages that are subclasses of the declaration class can only access their own inherited members protected.

public class StoryBook extends Book {
    public StoryBook() {
        System.out.println(this.variable); // this.variable is visible
    }
}

... but not inherited elements of protectedobjects.

public class StoryBook extends Book {
    public StoryBook() {
        System.out.println(this.variable); // this.variable is visible
    }

    public boolean equals(StoryBook other) {
        return this.variable == other.variable; // error: StoryBook.variable is not visible
    }
}

Also take taken in this article

?

+2

All Articles