How does encapsulation help in programming




















Cite this Article Format. Bolton, David. Definition of Encapsulation in Computer Programming. What Are Interest Groups? Definition and Examples. What Is an Enum in Programming Languages? Comparing Popular Programming Languages. What Are Individual Rights? Your Privacy Rights. To change or withdraw your consent choices for ThoughtCo. At any time, you can update your settings through the "EU Privacy" link at the bottom of any page. These choices will be signaled globally to our partners and will not affect browsing data.

We and our partners process data to: Actively scan device characteristics for identification. I Accept Show Purposes. Methods and attributes that use the public modifier can be accessed within your current class and by all other classes. Public methods and attributes become part of the public API of your class and of any component in which you include them. That is almost never a good idea for any attribute, and you should think twice before you use this modifier on a method.

Also keep in mind, that sooner or later this method will be used by some part of your application that will make it hard to change or remove it. In general, your public API should be as lean as possible and only include the methods which are intended to be used by other parts of the application or by external clients.

The CoffeeMachine class has to be public because it represents the interface of the coffee machine. The constructor and the brewCoffee and addBeans methods can be called by other classes to create a new instance of the CoffeeMachine and to interact with it by adding coffee beans or by brewing a fresh cup of coffee. 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. It's especially important if you develop open-sourced gem or application.

Inheritance OOP is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behavior. Class Human is a parent class for Man and Woman.

Because Man and Woman are inherited from Human , they have all methods that Human has: walk and breathe. There is a easy way to define if you should use inheritance or composition. It's called is-a vs has-a. We use inheritance when you can describe relation between parent and child class as is-a , for example:. Let's see another example. Superman - it's still human, but it has has-a superpower.

In this case we can use composition:. Superman is still human, he walks walk and breathes breathe , but now he has has-a an ability to fly. That's because we included Superpower module into class. Usually developers create one controller and inherit other controllers from it. This relation also fits perfectly to is-a relations. For example UsersController it's a controller of our application - ApplicationController.

Couple more words about inheritance. In some languages multiple inheritance is possible. One class can be inherited from couple more parent classes. Multiple inheritance isn't possible in Ruby. We can inherit class just from one parent class. Imho, it was right decision by Matz, because in many cases multiple inheritance it's a bad idea which reveals problem with proper design of application.

If we want to extend class by some methods, we always can use mixins and include module with those methods. Speaking about inheritance, we have to discuss method super , which allows to call method from parent inside child class. Superman is inherited from Man class. Let's imagine that for regular man power is equals to 10 points.

We want Superman to have x1.



0コメント

  • 1000 / 1000