What is the use of Protected modified when default works fine

S

Suresh Reddi

Hi,

I have doubt regarding protected modifier.
as per definition, the visibility of "protected" and "default" are

Visible to the package and all subclasses (protected).
Visible to the package-the (unfortunate) default. No modifiers are
needed.

My doubt is when "default" field/method is visible to package, it might
also visible to sub-class as well. then why "protected" is required?

Or is it so that, one class can become sub-class of a super-class from
other package?

Thanks in advance.
Suresh
 
A

Adam Maass

Suresh Reddi said:
Hi,

I have doubt regarding protected modifier.
as per definition, the visibility of "protected" and "default" are

Visible to the package and all subclasses (protected).
Visible to the package-the (unfortunate) default. No modifiers are
needed.

My doubt is when "default" field/method is visible to package, it might
also visible to sub-class as well. then why "protected" is required?

Or is it so that, one class can become sub-class of a super-class from
other package?

"default" is visible to all classes in the same package, whether subclass of
the current class or not. "protected" is visible to all classes in the same
package, as well as subclasses in other packages.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top