This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Am i getting the difference between Protected and Default Correct ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Am i getting the difference between Protected and Default Correct ?" Watch "Am i getting the difference between Protected and Default Correct ?" New topic
Author

Am i getting the difference between Protected and Default Correct ?

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

From my study of Access modifiers i have come to the following conclusion about the Protected and the default access modifier:
please let me know if i am rite or wrong or missing anything

Protected members:
Avaliable inside class + These members are available to all derived classes and instance(objects) of these classes independant of the package they are in
+unavailable to non derived classes in a different package.

Default members:
Avaliable inside class + These members are available to all derived classes and instance(objects) of these classes but are restricted to the package of the class they are declared in.


Don’t look where you fall, but where you slipped
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

What do you mean by members being available to instance of the derived class? can you please explain.


Piyush
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Piyush Joshi wrote:What do you mean by members being available to instance of the derived class? can you please explain.


I mean the objects of these classes can access protected members example


Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Adam,


Default members:
Avaliable inside class + These members are available to all derived classes and instance(objects) of these classes but are restricted to the package of the class they are declared in.


Default members are also available to any non-derived class in the package. Think of default access as package level access.

Thanks,
Badal


Blog: http://badalchowdhary.wordpress.com/
Twitter: http://twitter.com/badalrocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This tutorial page shows it with a table:

The Java Tutorials wrote:
Access Levels

Modifier    Class Package Subclass World
public        Y     Y       Y        Y
protected     Y     Y       Y        N
no modifier   Y     Y       N        N
private       Y     N       N        N



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Jesper de Jong wrote:This tutorial page shows it with a table:

The Java Tutorials wrote:
Access Levels

Modifier    Class Package Subclass World
public        Y     Y       Y        Y
protected     Y     Y       Y        N
no modifier   Y     Y       N        N
private       Y     N       N        N



thanks for the post
I am having a bit difficulty comprehending this table , Looking at the "no modifier" row it states that for subclass = "N". But if subclasses are in the same package then the fields are available ... Furthermore could you clarify the WORLD ?? Does the WORLD mean "non subclasses in a different package"
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Class means "Everywhere inside the current class". Package means "Every class within the current package". Subclass means "Every subclass of this class". World means "Every class".

So if a field has no modifier, it can not be reached by "every subclass of this class", but it *can* be reached from "every class within the current package", which is what the Y and the N mean in that row.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Adam Zedan wrote:
I am having a bit difficulty comprehending this table , Looking at the "no modifier" row it states that for subclass = "N". But if subclasses are in the same package then the fields are available ...

Yes, because in the "package" column is a "Y"...

Adam Zedan wrote:
Furthermore could you clarify the WORLD ?? Does the WORLD mean "non subclasses in a different package"

It means any class, anywhere.

You should read the table as: if there is one "Y" that applies to the situation, then the field or method is accessible. You seem to be reading it the other way around: if there's an "N" that applies to the situation it is not accessible.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Thanks for pointing that out ... appreciate it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Am i getting the difference between Protected and Default Correct ?
 
Similar Threads
New Doubt: from SCJP6.0 K&B MockExam-1
Protected and default access of a class member
access
Protected access modifier
Protected members