• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Modifiers little bit confusion... Help with World..

 
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
Coming from a c++ background i was surprised to read that protected fields in a class were accessible to the instances of the class.
I was however a bit confused b/w the default (no access modifier) and the protected modifier. Could anyone please give a simple explaination on their difference.
I did look up on the internet and even oracle http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html has something like this

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

could you kindly tell me what the world is ?? thanks...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means everybody. Any class can use that method/variable.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Zedan wrote:Coming from a c++ background i was surprised to read that protected fields in a class were accessible to the instances of the class.



Why is this surprising? If private fields of a class are accessible to the instances of the class, why aren't protected fields accessible?

Henry
 
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

Henry Wong wrote:

Adam Zedan wrote:Coming from a c++ background i was surprised to read that protected fields in a class were accessible to the instances of the class.



Why is this surprising? If private fields of a class are accessible to the instances of the class, why aren't protected fields accessible?

Henry



Private fields accessible by instances classes ?? i am a bit confused... I tried

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Zedan wrote:
Private fields accessible by instances classes ?? i am a bit confused... I tried



I think we have a definition mismatch here. Can you define to us what you think an "instance of a class" means?

Henry
 
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

Henry Wong wrote:

Adam Zedan wrote:

I think we have a definition mismatch here. Can you define to us what you think an "instance of a class" means?

Henry



Instance of a class example:
class simple_class
{
.......
.......
}

void main(String[] args)
{
//create an instance/object of simple_class
simple_class inst = new simple_class();

//now inst is an instance or an object of simple_class
}

what did you have in mind ??

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. And instances of simple_class can access private members of simple_class. But in your earlier example, you weren't in simple_class - you were trying to access the private member from the Test class.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically, a private field or method can be accessed by any "instance of the class". This means that any method of any instance of that same class can access that private field or private method -- or the class itself (if the private field or method is static). We are talking about the same class.

In your example, you seem to imply that any class (even a different class type) can access any private field of an instance that it has an instance of. And no, that is not allowed in Java -- for private, default, or protected fields or methods.

Henry
 
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

Matthew Brown wrote:That's right. And instances of simple_class can access private members of simple_class. But in your earlier example, you weren't in simple_class - you were trying to access the private member from the Test class.




hmm.. I think this is a case of misunderstanding earlier i assumed you were saying something like



But now i guess you mean something like this
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Zedan wrote:
hmm.. I think this is a case of misunderstanding earlier i assumed you were saying something like



I guess the next question is... what did you mean by this?

Adam Zedan wrote:Coming from a c++ background i was surprised to read that protected fields in a class were accessible to the instances of the class.



As my interpretation of what you meant was clearly incorrect.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic