LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2007, 04:46 AM   #1
sleight
LQ Newbie
 
Registered: Feb 2006
Location: Germany
Distribution: Ubuntu (breezy)
Posts: 3

Rep: Reputation: 0
Quick Java Questioin: protected access


Hi,

I'm new to Java (but not new to programming) and am a little bit confused, how Java handles the proteced modifier. What is the reason, why Java let's you access protected members even outside of the class-hierarchy? And what is the difference to public?

Code:
class A{
    protected int _i;
}


public class Main {
    public static void main(String[] args) {
        A a = new A();
	int i = a._i;  /* Ok! But why? And what is then the difference to public? */
    }
}

Thanks for your time

Last edited by sleight; 02-17-2007 at 04:48 AM.
 
Old 02-17-2007, 06:48 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Normally Protected is only available within the class hierarchy, but if you are including the two classes in the same file then, if I remember correctly, it is considered to be in the same package which allows you to cross class hierarchies. Normally the two classes would be in different files and so their access would be restricted (unless class Main inherited class A)
 
Old 02-18-2007, 05:55 AM   #3
sleight
LQ Newbie
 
Registered: Feb 2006
Location: Germany
Distribution: Ubuntu (breezy)
Posts: 3

Original Poster
Rep: Reputation: 0
Ah ok, thank you! I guess Sun will have its reasons for such confusing and "inconsistent" access modifiers..., even if I fail to see them. But nevertheless, I do understand at least the syntax now

Regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple C++ string questioin, i hope Ha1f Programming 2 10-12-2005 08:23 PM
access an MS Word password protected doc axelmang Linux - General 12 10-18-2004 10:26 AM
Quick Java question Nylex Programming 2 01-26-2004 12:44 PM
quick question - java Laptop2250 Programming 2 10-15-2003 09:56 PM
2 Quick Java Questions Crashed_Again Programming 13 09-10-2003 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration