• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Am i getting the difference between Protected and Default Correct ?

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Saloon Keeper
Posts: 13984
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing that out ... appreciate it..
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic