File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Access Modifiers little bit confusion... Help with World.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access Modifiers little bit confusion... Help with World.." Watch "Access Modifiers little bit confusion... Help with World.." New topic
Author

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

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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...


Don’t look where you fall, but where you slipped
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It means everybody. Any class can use that method/variable.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

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

Joined: Jun 10, 2011
Posts: 124

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 ??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40


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

Joined: Jun 10, 2011
Posts: 124

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
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers little bit confusion... Help with World..