2
Modifier    Class   Package Subclass    World
public      Y          Y       Y           Y
protected   Y          Y       Y           N
no modifier Y          Y       N           N
private     Y          N       N           N 

No modifier (default modifier) is accessible from the same package only and not with the subclass.

What if the subclass is in the same package? Will it be accessible to the subclass?

Maroun
  • 94,125
  • 30
  • 188
  • 241
kevin
  • 328
  • 4
  • 15
  • 33
  • 2
    Why the down-votes? This is a valid and answerable question. +1 as partial compensation. – Andrew Thompson Dec 15 '12 at 09:37
  • > **Default - Same Package only** (which means any class/sub-class within same package). You may check [this](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private/13102616#13102616) – Ravi Dec 15 '12 at 09:39
  • @var___ so class1 and class 2 are in different packages, if class2 extends class1, It cannot access the default modifier variable/method? – kevin Dec 25 '12 at 13:19
  • 1
    @masood exactly. Even you can try it ;) – Ravi Dec 25 '12 at 13:21
  • @var___ ok.. thanks! do want to put an answer or I accept some other answer? whhich one from below is correct? I dont understand either one – kevin Dec 25 '12 at 14:04
  • @masood hahahahaha... you don't get the answer and you accepted !! – Ravi Dec 25 '12 at 14:19
  • @masood check my answer, i have posted just right now, to make you clear. – Ravi Dec 26 '12 at 06:18

2 Answers2

2

What if the subclass is in the same package? Will it be accessible to the subclass?

Yes. The "and not with the subclass" is just "it's not automatically accessible to the subclass" - it's not like it's explicitly prevented from being accessible to subclasses.

See the Java Language Specification section 6.6 for precise details. In particular:

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

No modifier (default modifier) is accessible from the same package only and not with the subclass.

yes it is accessible, no modifer(default modifier) is accessible in all the class's with in the same package.

    pkg1;
    class CWithDefAccess{
    }

    pkg1;
    public class anotherclass {
       //can access CWithDefAccess as they are in the same package
    }

    pkg1;
    public class Foo extends CWithDefAccess {
    //can access CWithDefAccess as they are in the same package
    }

    pkg1; 
    public class Baz extends anotherClass{
    //can access CWithDefAccess as they are in the same package
    }
PermGenError
  • 45,977
  • 8
  • 87
  • 106