How can we achieve encapsulation




















But for simple getter and setter accessor methods this is potentially very confusing for clients of your code. If you can't reconcile things and calling setValue on an Encap is never the right thing to do, i would recommend overriding the method, commenting it liberally and have it do nothing, or throw an exception to indicate to the client that they're doing something that doesn't make sense. How are we doing? Please help us improve Stack Overflow.

Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 7 years, 5 months ago.

Active 7 years, 5 months ago. Viewed times. Improve this question. If we want to use the data stored in an object to perform an action or calculate a derived value, we define a method associated with the object which does this. Then whenever we want to perform this action we call the method on the object. We consider it bad practice to retrieve the information from inside the object and write separate code to perform the action outside of the object. Some languages have features which allow us to enforce encapsulation strictly.

Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirement. If we wish to make the variables read-only then we have to omit the setter methods like setName , setAge , etc. Testing code is easy: Encapsulated code is easy to test for unit testing. This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Basics of Java. Operators in Java. Packages in Java. The brewCoffee method shows another benefit of the different access modifiers. You can not only use it to hide information, but you can also use to support abstraction. The public brewCoffee method abstracts the internal details of the brewFilterCoffee and brewEspresso methods, which are both private.

The access modifiers ensure that an external class can only call the abstraction provided by the brewCoffee method, but not the internal methods. Here you can see an overview of the different access modifiers and the accessibility of the attributes or methods. The Coffee class provides a good example of the information-hiding mechanism. It represents a drink that was brewed by the CoffeeMachine. The class uses two private attributes to store information about the CoffeeSelection and the quantity of the drink.

The access modifier private makes both attributes inaccessible for other classes within the same or other packages. If you want to get information about the current state of the object, you need to call one of the public methods. The getSelection method provides read access to the selection attribute.

It represents the kind of coffee that was brewed by the CoffeeMachine , e. The available quantity of a drink changes over time.

After every sip you take, your cup contains a little bit less. Therefore, I implemented a getter and setter method for the quantity attribute. If you take a closer look at the setQuantity method, you can see that I also implemented an additional validation. If the coffee is especially delicious, you might drink it until your cup is empty. So the quantity of the Coffee has to be greater or equal to zero.

Encapsulation is one of the core concepts in object-oriented programming. It describes the bundling of data and methods operating on this data into one unit.



0コメント

  • 1000 / 1000