aspose file tools*
The moose likes Beginning Java and the fly likes protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected" Watch "protected" New topic
Author

protected

lekha phijo
Greenhorn

Joined: Oct 09, 2003
Posts: 29
I am not convinced about the behaviour of protected keyword.
protected variable should not be available to other classes in same package.
So that makes in mandatoty to create packages.
any comments
Gregor Slokan
Greenhorn

Joined: May 31, 2002
Posts: 5
Hi!
As you presumably know, anythibg declared as private is visible only in the class itself. If you declare them as public, they can be accessed from any class in any package.
But if you want things to be accessed also in successors of your class, you declare it private. So 'protected' basicly means: "only me and my subclasses can see this"
lekha phijo
Greenhorn

Joined: Oct 09, 2003
Posts: 29
Yes nanook,
i agree protected means only me and my subclasses. But protected behaves as public for classes in same package.I am not convinced that OOPS is satisfied here.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Why you are not convinced?!! If you think that a variable should not be available to other classes in the same or different package then mark that variable with the private modifier.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nanook --
You've already been warned once to change your display name. Please comply or we'll have to close your account.
Lekha and Nanook --
You're unfortunately both incorrect in your expectations. "protected" means access from subclasses and from other classes in the same package. Java does not have an access level that means only subclasses.
Note that the access levels "nest". Each one opens things up a bit more. There are no access levels that both add new permissions and revoke permissions from a lower level.
  • private -- one class only.
  • default -- all classes in a package.
  • protected -- all classes in a package plus subclasses.
  • public -- all classes.


  • [Jess in Action][AskingGoodQuestions]
    lekha phijo
    Greenhorn

    Joined: Oct 09, 2003
    Posts: 29
    private -- one class only.
    default -- all classes in a package.
    protected -- all classes in a package plus subclasses.
    public -- all classes.
    That I know is how java deals with access levels.
    But my problem is If I have to give access only to child classes then i have to have all unrelated classes in seperate package
    .Is that the only solution.
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Originally posted by lekha phijo:

    But my problem is If I have to give access only to child classes then i have to have all unrelated classes in seperate package
    .Is that the only solution.

    Yes.
    lekha phijo
    Greenhorn

    Joined: Oct 09, 2003
    Posts: 29
    Thank you.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: protected