Difference between the default access specifier and protected access specifier in java

故事扮演 提交于 2019-12-29 13:12:14

问题


I was trying to learn java and when I went through access specifiers I had a doubt. What is the difference between the default one if none is specified and the protected access specifier in java?


回答1:


The protected specifier allows access by all subclasses of the class in question, whatever package they reside in, as well as to other code in the same package. The default specifier allows access by other code in the same package, but not by code that is in subclasses residing in different packages. See Java Language Specification Section 6.6.

EDIT: Per request of Michael Schmeißer (so others don't have to read through the comments or follow a link to find this): all members of interfaces are implicitly public. It is, in fact, a compile-time error to specify any access specifier for an interface member other than public (although no access specifier at all defaults to public access). Here's the full set of rules from the JLS for class members (see the above link for the rules for packages, top-level classes and interfaces, and arrays):

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, then access is permitted.

  • All members of interfaces are implicitly public.

  • Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:

  • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

  • Access is correct as described in §6.6.2. (This clause refers to the rules that allow derived classes to access protected members of superclasses; §6.6.2 starts: "A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object." It then elaborates on that.)

  • Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

  • 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.




回答2:


This Java tutorial may be of some use to you.

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



回答3:


Protected Access specifier - there are two ways to access protected data

  1. The protected data members,protected methods of a class will be visible to the other Classes if they resides in same package

  2. Using Inheritance

    means we can use the protected data of that class by Inheriting that class.

Default access specifier- Only one way to access default data

Default restricts the access only to package level , even after extending the class having default data members ,we won't be able to access.

Example

To check it for default remove protected keyword for int x in ProvideProtected , a compile time error will be generated.

        1. SuperClass

        package nee.superclass;

        public class ProvideProtected {
            protected int x=800;

        }

        2.Subclass


        package nee.subclass;

        import nee.superclass.*;

        public class AccessProtected extends ProvideProtected 

        {   
        public void accessProtected()
            {
                System.out.println(x);
            }

            public static void main(String[] args) {
                AccessProtected obj=new AccessProtected();
                obj.accessProtected();

            }

        }



回答4:


Protected Access Modifier :- Anything marked as protected is visible within the same package and also visible in the subclass .

Default access :- Default is not a keyword . It applies when no access modifier is specified . It is basically a package level modifier. Anything which is having such an access is visible in the same package .

Now the difference can be explained better with the help of an example

package p1

public class A
{

protected void fn()
{

}

} 

package p1

public class B
{

A a1 = new A();

a1.fn();// fn() is visible inside the same package

}

}

Now we come to a subclass in different package

package p2

public class D extends A

{

void test()

{

A a1 = new new A();

    //a1.fn() -->  would give compilation error
fn();

super.fn();

}

}

fn(), super.fn() will not give an error.

So, the difference is in the subclass the method can't be called via reference of super class. Either You can call it directly or use super.

Note that super.fn() must be a part of non-static method.




回答5:


Long story short:

default member is available in all the other classes of the same package;

protected member is available in all the other classes of the same package and in any other class of any other package as long as that class extends the class containing your protected member.



来源:https://stackoverflow.com/questions/9867976/difference-between-the-default-access-specifier-and-protected-access-specifier-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!