kevin
kevin

Reputation: 326

java acces modifiers

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?

Upvotes: 2

Views: 128

Answers (2)

PermGenError
PermGenError

Reputation: 46408

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
    }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500465

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.

Upvotes: 2

Related Questions