Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private and Protected

 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
What the main difference between private and protected? What is the meaning of private protected int seconds = 23;? If I have private integer seconds = 23; in private class Hello, Can I change the value of integer seconds when I am in the private class Hello?
Thanks
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Shn:
Hello
What the main difference between private and protected? What is the meaning of private protected int seconds = 23;? If I have private integer seconds = 23; in private class Hello, Can I change the value of integer seconds when I am in the private class Hello?
Thanks

You should get an error , because using private and protected is not allowed in combination. What i found interesting that JBuilder allowed you to compile but though a warning saying that they took out private and left just protected.
Yes you can change the value of youru seconds variable in that class. What you can't do is access that variable outside it. you can define a public geter that will change that value for you. This is called encapsulation.
 
Junilu Lacar
Bartender
Posts: 7568
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Val Dra:
you can define a public geter that will change that value for you. This is called encapsulation.

Well, not really. Check out this article: "Encapsulation is not Information Hiding"
Junilu
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i read that article last week .
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sense a holy war.
 
Junilu Lacar
Bartender
Posts: 7568
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Curwen:
I sense a holy war.

I like to think of it as more of a crusade, really. I may have to work with some of these folks
I find it frustrating sometimes to see how much attention is given to the mechanics of the language and yet little given to the principles of Object Orientation. If you look at some of the code in the Java Tutorial, you can't help wonder sometimes if the people who wrote the sample programs knew anything about object orientation or not.
It's even more amazing (and frightening) when I show people the initial UML diagrams and code in the first chapter of "Refactoring" and ask them if it is object-oriented and they reply "Yes."
It seems that even though Java is supposed to be more OO, it still takes more effort to write OO code than it does to write non-OO code.
Junilu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic