SAP ABAP Encapsulation - SAP ABAP

What is Encapsulation in sap abap? What work it does?

Encapsulation is an Object Oriented Programming (OOP) concept that does the same work as in Java programming language. It encloses the data and functions that manipulate the data, which provides security from outside interference. Encapsulation is nothing but data hiding. It restricts the visibility of data for security reasons. Encapsulation helps the developer to access the data without affecting others. The main difference between encapsulation an abstraction is encapsulation helps in enclosing the data and functions together, where abstraction is a process of hiding the implantation details from the user and revealing the interfaces.

In ABAP the base for encapsulation is the Class. ABAP supports this which is called user-defined type. As we knew in the previous sessions, a class can contain private, protected and public members. Here the data types referred in a class are mainly private.

Encapsulation by Interface

Encapsulation is a process of having one attribute and method that could be modified in different classes. The class can be separated by hiding the data and method. A class supports the interface by providing implementation of the methods.

Encapsulation by interface is used when one method is created performing different functions in different classes. In this the name of the method is not changed and the same method will have to be implemented in different class implementations.

Example

You can see the example given below with an Interface by name inter_1. We have declared attribute and a method method1. Two classes have been mentioned that is Class1 and Class2. So implementation can be done from the method ‘method1’ to both the class implementations. Implementation can takes place differently in different classes from the method ‘method1’. In the start-of selection, we have created two objects Object1 and Object2 for two classes. Then, when we call the method by different objects, functions will get declared in separate classes.

The above code produces the following output −

Encapsulated classes do not depend on the outside world. However, when they interact with the outside clients, these are controlled through a stabilized public interface. That is, an encapsulated class and its clients are loosely coupled. For the most part, classes with well-defined interfaces can be used into another context. When designed correctly, encapsulated classes become reusable software assets.

Designing Strategy

We have learnt from the experiences that class members become private through encapsulation by default unless and until we really bound to expose them. That is just good encapsulation. This knowledge is applicable to to data members more often and it also applies equally to all members.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAP ABAP Topics