• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is Encapsulation  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class MyMarks {
private int vmarks = 0;
private String name;
public void setMarks(int mark)
throws MarkException {
if(mark > 0)
this.vmarks = mark;
else {
throw new MarkException("No negative
Values");
}
}
public int getMarks(){
return vmarks;
}
//getters and setters for attribute name goes here.
}

In the code above , instead of private variables can I have access specifier as protected ? Will it still be encapsulation ?
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the variables are private, which other classes can access them directly without using the "get" function?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general it means hiding implementation details from the client.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote: instead of private variables can I have access specifier as protected ? Will it still be encapsulation ?


As far I know, Encapsulation is Specific to a class , another class(say user) dont have the rights to change/affect the data(encapsulation). if you make the variable as protected,it visible to a user(if he extends that class), which violate encapsulation. when you say protected, it is mostly related to an Abstraction
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
encapsulation will "shield" your instance variables (state)

say you have an Animal object (class), that may have ints for "eyes", "legs" and so on

You dont want to be setting these values directly elsewhere in your application, for example you dont (but can) want to do:

Animal a = new Animal();

a.eyes = 2

Thats not nice, you want to use encapsulation to shield that variable. So you would have accessors (getters and setters) which will set/retrieve the values for you, so you can say:

Animal a = new Animal();
a.setEyes(2);

That means that every time you set the number of eyes, it goes thru that accessor.

Another better example may be if you have a House object, such as :

House h = new House();
h.setPostcode("ABC123");

At least then, on your setPostcode method(accessor) in your House class, you could introduce a regex, or to format to upper or lower case etc. It introduces a single point of failure, so providing everyone uses the acessors, they should be guaranteed to get the behaviour they expected
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:In the code above , instead of private variables can I have access specifier as protected ?  Will it still be encapsulation ?

Protected says that you can access the variable/method from a subclass. (Unfortunately Java also adds package private to protected members, so it's not that protected but you should protected as if it was like that).
That means that if you have a protected variable, you still can access it directly from a subclass and thus it is not encapsulated.

Encapsulate means that you hide the variable so that it can only be accessed from within the class itself. The outside world should only be able to access them via getters and setters.
One more thing: encapsulation does not mean that you have to expose every variable via getters and setters. It is a good OO practice to only use getters and setters when needed to avoid unexpected changes to your code.
For more information I found a slidshare of the book I'm currenty reading for my OCA exam which explains in more detail about the access modifiers and encapsulation:

https://www.slideshare.net/ibrahimkurce/oca-java-se-8-exam-chapter-4-methods-encapsulation

Slide 15-26 talks about the access modifiers like protected and private.
Slide 56-58 covers encapsulation

Hopefully it helps you out.

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your insights Arco. I doubt the OP will check back on this thread though, since it's 7 years old ;)

Arco Brouwer wrote:One more thing: encapsulation does not mean that you have to expose every variable via getters and setters.

A cow for this. Only expose properties if the outside world really needs them!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!