Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

default vs protected access  RSS feed

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
It was my mistake.

Thanks for clearing my doubt
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!