File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected

 
lekha phijo
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    lekha phijo
    Greenhorn
    Posts: 29
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 24204
    34
    Chrome Eclipse IDE Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 29
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic