1

According to the second table of documentation (http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) a member with no identifier is not visible to a subclass.
But, when I run the following sample code, "1" (content of b) is printed!

class Class1{
    private int a=0;
    int b=1;
    protected int c=2;
    public int d=3;
}

class Class2 extends Class1{ }

public class HelloWorld{

     public static void main(String []args){
        Class2 klass=new Class2();
        System.out.println(klass.b);
     }
}

If a member, with no access modifier, is not accessible from a subclass why is it printed in this example?
It should throw an error, like in private access modifier, shouldn't it?

LiTTle
  • 1,811
  • 1
  • 20
  • 37
  • No, default access modifiers accessible from if subclass in the same package – Ugur Artun May 30 '13 at 05:45
  • 1
    a member with no identifier is not visible to a subclass. continued-> " if they are not in the same package " , if they are in same package or in same declared file , that means the same case , it is able to access it . – anshulkatta May 30 '13 at 05:53
  • [Here](http://stackoverflow.com/a/33627846/276052) is a version of the table that is slightly more clear. – aioobe Nov 13 '15 at 12:26

5 Answers5

5

Look like both class(Class1 & Class2) is in same package as well same class HelloWorld it self and default modifier is visible with in class or package.

default modifier or no modifier has significance in java, it is not same as private and it's access level is well defined in documentation.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
2

The Java Language Specification writes:

  • If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

  • ...

  • A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:

    • If the member or constructor is declared public, ....

    • Otherwise, if the member or constructor is declared protected, ...

    • Otherwise, if the member or constructor is declared private, ...

    • Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.

So it doesn't matter whether the access is from a subclass, all that matters is the package.

Since Class1 and Class2 are declared with default access, HelloWorld must be in the same package with them in order to compile.

meriton
  • 68,356
  • 14
  • 108
  • 175
1

It's whats known as package private. Any classes including sub-classes in the same package have access to the default modifier.

See extended answer at: In Java, difference between default, public, protected, and private

Community
  • 1
  • 1
Jeremy Unruh
  • 649
  • 5
  • 10
0

If you modifier is default you have access through out its package. If the sub class is same as the super class means you can get the result. For further brief explanations about modifiers follow my blog

Hariprasath
  • 828
  • 4
  • 15
  • 41
0

In Same package, Child class can access all the member element of its parent class and thats why it is able to print the SOP statement.Because default modifier is know as package private means it can be accessed in same package.

Yes when you go in another package, with child class object again you can access the member variable with default modifier.

might be this link will help you to understand more.Access modifier in java with example