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.
Joined: Dec 12, 2005
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.
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.
Joined: Dec 12, 2005
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.
Joined: Oct 15, 2005
Hi Jeff, I hv executed this program. 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 ]