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

Is there any difference between synchronized methods and synchronized statements?

594


What's the default access specifier for variables and methods of a class?

525


What is the difference between length and length () in java?

535


What is java virtual machine and how it is considered in context of java’s platform independent feature?

661


What happens when main () method is declared as private?

658






What is meant by local variable and instance variable?

606


Are global variables initialized to zero?

521


How to perform selection sort in java?

592


What is the benefit of inner classes in java?

620


What is a two-pass assembler?

613


What are the differences between getting and load method?

548


how to convert Java project into Maven ?

597


What is object data type?

575


What are the java ide's? Explain

536


Why do people says “java is robust”?

568