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

Where and how can you use a private constructor?

761


State two differences between C and Java.

959


Explain about the interpreter in java?

833


Can a class with private constructor be extended?

708


What is tochararray in java?

709


If two threads have same priority which thread will be executed first ?

1152


How would you use Bubble Sort to sort the number of elements?

822


Is arraylist ordered in java?

778


Difference between string, stringbuffer and stringbuilder?

819


What if the main() method is declared as private? What happens when the static modifier is removed from the signature of the main() method?

946


What is the default value of float and double datatype in java?

775


Is array a class in java?

733


Tell me are there implementations for sorting and searching in the java libarary?

824


What does the exclamation mark mean in java?

927


What is class forname?

737