Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Am i getting the difference between Protected and Default Correct ?

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by members being available to instance of the derived class? can you please explain.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out ... appreciate it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic