Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between protected and having no access modifier [closed]

When declaring a variable in Java,what's the difference between "protected" and having "no access modifier"? Is it the same?

like image 983
Siyno Avatar asked Jul 24 '13 16:07

Siyno


People also ask

What is the difference between protected and default access modifier?

Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. Protected: The access level of a protected modifier is within the package and outside the package through child class.

What is a protected access modifier?

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

What will happen when a property of a class has no access modifier?

Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default. The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

What is the difference between access and non access modifiers?

The public keyword is an access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors. We divide modifiers into two groups: Access Modifiers - controls the access level. Non-Access Modifiers - do not control access level, but provides other functionality.


1 Answers

It isn't same.

Both protected and no access modifier variables are accessible in the same package, but protected variables can be accessed by a subclass instance anywhere (in any package).

like image 107
ThiepLV Avatar answered Oct 23 '22 09:10

ThiepLV