File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default vs protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "default vs protected access" Watch "default vs protected access" New topic
Author

default vs protected access

Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Why is default more restrictive than protected? this is what is read in books.
default members can be accessed via the reference.member but for protected you cant do this.

Please explain

Check this

Parent.java


Child.java


Use.java


This code wont work if int a = 10 is made protected.

I think default can do certin things which protected cant
also protected can do certain things which default cant

So why is it said default is more restrictive?

[ December 23, 2005: Message edited by: Jeff Kumar ]
[ December 23, 2005: Message edited by: Jeff Kumar ]

SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
when you are saying default, it means that it is package level accecibility. it is NOT available outside of the package. try it.

in contrast to that, protected is visible outside package also. But there is important difference here.

protected has something to do in subclasses. so a protected member in superclass is also visible in subclass of that defined in another package. where as default is not.

I think you can try out different situations with some sample code. just highlighting those.

(1) superclass in package A and subclass in package B. try default member
(2) superclass in package A and subclass in package B. try protected member
(3) try accessing a default member (defined in package A ) in package B.
(4) try accessing a protected member (defined in package A ) in package B.

I have my own understanding for above.
default is 'public' in same package and 'private' in all other packages.
protected is just protected regardless of any package.
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
I know what default means. only accessible in the same package
I am just asking why do boooks say protected is more accessible than default even though as per my example shows that protected can only be accessible to subclasses but default has no such restriction.

So... they are really not more accessible or more restrictive.
On certain situations default is more accessible but in other siutations protected is more accessible.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163

This code wont work if int a = 10 is made protected



Hi jeff,
Even if a has protected access in Parent class,the given program will work and gives output 10.


Why is default more restrictive than protected?


Yes, default is more restrictive than protected.
'default' members can be accessed by the otherclasses in the same package only.

'protected' members can be accessed by the otherclasses in the same package
+
subclasses in any package.

Hence 'default' is more restricted than protected.
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
I have posted after trying the code
It wont even compile if a is made protected. Please try again.

You can only access a protected variable via inheritance and not via reference variable.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Hi Jeff, I hv executed this program. Output 10








Output: 10


You can only access a protected variable via inheritance and not via reference variable


Sub class can access super class protected member through inheritance only. and not via superclass variable.
If the child class method uses superclass reference to access the superclass protected member, it will be compilation error.

In the given program extends class is not used, so there is no parent child relation between the classes 'Parent' and 'Child'.

Use class main method is calling 'useParent()' (public)method of 'Child' class.
UseParent() method is accessing 'a' (protected member) in 'Parent' class, there is no compilation error because, both the classes 'Parent','Child' both are in same package 'cert'.
A protected member can be accessed by any class in the same package.
[ December 24, 2005: Message edited by: Naresh Kumar ]
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
You are right.
It was my mistake.

Thanks for clearing my doubt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default vs protected access