What is "encapsulation"

Encapsulation is a property of the system that allows you to combine data and methods that work with it in a class and hide implementation details from the user, revealing only what is needed for subsequent use.

The goal of encapsulation is to get away from the implementation dependency of the external interface of a class (something that other classes can use). So that the slightest change in the class does not entail a change in the external behavior of the class.

Let's imagine for a moment that we found ourselves at the end of the century before last, when Henry Ford had not yet invented the conveyor belt, and the first attempts to create a car faced criticism from the authorities that these smoking monsters pollute the air and scare horses. Imagine that in order to drive the first steam car, it was necessary to know how the steam boiler works, constantly toss up coal, monitor the temperature and water level. In this case, to turn the wheels, use two levers, each of which turns one wheel separately. I think we can agree that driving a car at that time was very uncomfortable and difficult.

Now let's return to today to the modern wonders of the auto industry with an automatic transmission. In fact, in essence, nothing has changed. The gas pump still supplies gasoline to the engine, differentials provide wheel rotation at different angles, the crankshaft converts the forward motion of the piston into rotational motion of the wheels. Progress is different. Now all these actions are hidden from the user and allow him to turn the steering wheel and press the gas pedal without thinking about what happens to the injector, the throttle valve and the camshaft. It is the concealment of the internal processes occurring in the car that allows it to be effectively used even by those who are not a professional auto mechanic with twenty years of experience. This hiding in OOP is called encapsulation.

Example:

public class MyPhone {

    private int year;
    private String company;

    public MyPhone (int year, String company) {
        this.year = year;
        this.company = company;
    }

    private void openConnection() {
        // findComutator
        // openNewConnection ...
    }

    public void call() {
        openConnection ();
        System.out.println("Calling the number");
    }

    public void ring() {
        System.out.println("Tink-Tink");
    }
}

The private modifier makes the fields and methods of the class available only within this class. This means that it is impossible to access private fields from outside, just as there is no way to call private methods.

Hiding access to the openConnection method also leaves us free to change the internal implementation of this method, since this method is guaranteed not to be used by other objects and will not break their work.

To work with our object, we leave open the call and ring methods using the public modifier. Providing public methods for working with an object is also part of the encapsulation mechanism, since if you completely close access to the object, it will become useless.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed