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
What is class and object in java?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
What is the best definition for data?
What are the drawbacks of singleton class?
What is java ceil?
Can a class have more than one object?
How to implement a multithreaded applet?
What is a concrete classes? Is Java object class is concrete class?
Explain about method local inner classes or local inner classes in java?
What is difference between wait and notify in java?
Tell me how many ways are there to initialise an integer with a constant.
Is space a character in java?
What are the differences between string, stringbuffer and stringbuilder?
What is the use of arrays tostring () in java?
What is string args [] in java?