whats the point of OOP encapsulation?

hey guys. i was wondering if smb could help me out? im tired of reading countless articles that are copy pasted from each other so maybe a person and not a bot can explain stuff to me. in all the textbooks its said that "muh encapsulashun" is data hiding (whatever that means), using private fields, getters and setters. so now i have some questions: 1. who are we hiding the data from? lets say i have this:
class Book {
public String author;
public int isbn;
}
class Book {
public String author;
public int isbn;
}
so? from who i need to hide the "data"? 2. lets say i want to hide "data", shouldnt i put it somewhere more safe? i.e. database, vault, etc? 3. whats the point of having private fields if u can just call the getter/setter? book.author is the same as book.getAuthor(). so why overcomplicate everything? thanks in advance. im trying to understand whats the point of this stuff.
3 Replies
JavaBot
JavaBot4d ago
This post has been reserved for your question.
Hey @Fragmented friends! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
nikcho-kouhai
nikcho-kouhai3d ago
Generally speaking OOP has been frowned upon for a while now for a lot of reasons, encapsulation included. I suppose if I had to point out a positive at least in my book it's that it's easier to work with invariants and it just in general helps if you need to tie some logic to a certain state. For example:
class Product{
private double price;
//...class definition
public double setPrice(double price){
if(price <=0){
//bad stuff happens here...
}
}
class Product{
private double price;
//...class definition
public double setPrice(double price){
if(price <=0){
//bad stuff happens here...
}
}
There are of course other ways to do this. But having one and only one intersection where you can do something (like one setter method) can make it easier to follow the state of whatever you are modifying and. As a result of that tracking accesses to the field also becomes a lot easier since there is only once place from which you can do it from. For the most part it's just typical java stuff though. Java is pretty much like a paranoid parent that takes away just about anything you can possibly shoot yourself in the foot with. It puts you in an inflatable house where the wall sockets are closed off, any sharp objects are kept strictly locked away somewhere, there are four bodyguards around every window and door and accesses to the dangers of the knife or the outside world are provided only under some specific circumstances. to answer briefly, you pretty much hide the data from yourself. and to make that point I will say that private data isn't private at all. Once it's all compiled it's just out there. It's simply a logical construct that's supposed to help you write less buggy code (whether it actually helps you do that is a different matter tho)
JavaBot
JavaBot3d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?