what is the difference between the "protected and default"
modifiers?

Answer Posted / gsk

Protected:
The access specifier 'protected' can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. Members are accessible only to the class and its subclass(es). Subclass can exists within the same package or any other package.

Accessibility of method or field:
Accessible within the class: Yes
Accessible to subclass within the package: Yes
Accessible to subclass outside the package: Yes
Accessible to any other class within the package: Yes
Accessible to any other class outside the package: Yes


Default (no specifier): We can have a class or method with out specifier. Default members of a class are accessible only to the class and other classes within that package. If we do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.

Accessibility of method or field:
Accessible within the class: Yes
Accessible to subclass within the package: Yes
Accessible to subclass outside the package: No
Accessible to any other class within the package: Yes
Accessible to any other class outside the package: Yes

Is This Answer Correct ?    1 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the functionability stubs and skeletons?

927


How can I become a good programmer?

704


When is the finalize() called? What is the purpose of finalization?

849


Is list ordered in java?

795


Can a final variable be manipulated in java?

770


Explain the difference between serializable and externalizable in java?

779


how to handle exceptions in ejb?

2116


Difference between a class and an object?

787


What is split return?

701


What is the size of string?

800


What is main difference between variable and constant?

796


What does microservices mean?

727


What is the numeric promotion?

733


Name few "optional" classes introduced with java 8 ?

896


What is collections framework?

865