Software Development Exam  >  Software Development Notes  >  Learn and Master SAP ABAP  >  SAP ABAP - Encapsulation

SAP ABAP - Encapsulation | Learn and Master SAP ABAP - Software Development PDF Download

Encapsulation is an Object Oriented Programming (OOP) concept that binds together data and functions that manipulate the data, and keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
ABAP supports the properties of encapsulation and data hiding through the creation of user-defined types called classes. As discussed earlier, a class can contain private, protected and public members. By default, all items defined in a class are private.

Encapsulation by Interface

  • Encapsulation actually means one attribute and method could be modified in different classes. Hence data and method can have different form and logic that can be hidden to separate class.
  • Let's consider encapsulation by interface. Interface is used when we need to create one method with different functionality in different classes. Here the name of the method need not be changed. The same method will have to be implemented in different class implementations.

Example

The following program contains an Interface inter_1. We have declared attribute and a method method1. We have also defined two classes like Class1 and Class2. So we have to implement the method ‘method1’ in both of the class implementations. We have implemented the method ‘method1’ differently in different classes. In the start-ofselection, we create two objects Object1 and Object2 for two classes. Then, we call the method by different objects to get the function declared in separate classes.

Report ZEncap1. 

Interface inter_1.

   Data text1 Type char35.

   Methods method1.

EndInterface.

CLASS Class1 Definition.

   PUBLIC Section.

      Interfaces inter_1.

ENDCLASS. 

CLASS Class2 Definition.

   PUBLIC Section.

      Interfaces inter_1. 

ENDCLASS.

CLASS Class1 Implementation.

   Method inter_1~method1.

      inter_1~text1 = 'Class 1 Interface method'.

      Write / inter_1~text1.

   EndMethod. 

ENDCLASS.

 

CLASS Class2 Implementation.

   Method inter_1~method1.

      inter_1~text1 = 'Class 2 Interface method'.

      Write / inter_1~text1.

   EndMethod. 

ENDCLASS.

 

Start-Of-Selection.

   Data: Object1 Type Ref To Class1,

      Object2 Type Ref To Class2.

        

   Create Object: Object1, Object2.

   CALL Method: Object1→inter_1~method1,

                Object2→inter_1~method1. 

The above code produces the following output −

Class 1 Interface method 

Class 2 Interface method

Encapsulated classes do not have a lot of dependencies on the outside world. Moreover, the interactions that they do have with external clients 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 plugged into another context. When designed correctly, encapsulated classes become reusable software assets.

Designing Strategy

Most of us have learned through bitter experience to make class members private by default unless we really need to expose them. That is just good encapsulation. This wisdom is applied most frequently to data members and it also applies equally to all members.

The document SAP ABAP - Encapsulation | Learn and Master SAP ABAP - Software Development is a part of the Software Development Course Learn and Master SAP ABAP.
All you need of Software Development at this link: Software Development
73 videos|68 docs

Top Courses for Software Development

FAQs on SAP ABAP - Encapsulation - Learn and Master SAP ABAP - Software Development

1. What is encapsulation in software development?
Ans. Encapsulation in software development refers to the concept of bundling data and methods that operate on that data into a single unit or class. This allows for better organization, control, and security of the data within the class.
2. How is encapsulation achieved in SAP ABAP?
Ans. In SAP ABAP, encapsulation is achieved through the use of interfaces. Interfaces define a set of methods that a class must implement, ensuring that the data and behavior within the class are encapsulated and accessed in a controlled manner.
3. Why is encapsulation important in software development?
Ans. Encapsulation is important in software development because it helps to protect data from being accessed or modified in unintended ways. It also promotes code reuse, enhances security, and improves the overall organization and structure of the code.
4. Can encapsulation be achieved without interfaces in SAP ABAP?
Ans. While interfaces are the preferred method for achieving encapsulation in SAP ABAP, it is possible to achieve encapsulation through other means such as access modifiers like private, protected, and public. However, interfaces offer a more standardized and flexible approach to encapsulation.
5. How does encapsulation contribute to the overall design strategy in SAP ABAP?
Ans. Encapsulation plays a key role in the design strategy of SAP ABAP by promoting modular and reusable code. It helps to create a clear separation of concerns, making it easier to maintain and extend the codebase. Additionally, encapsulation enhances data security and reduces the risk of errors and conflicts in the code.
73 videos|68 docs
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

SAP ABAP - Encapsulation | Learn and Master SAP ABAP - Software Development

,

Exam

,

Objective type Questions

,

pdf

,

Semester Notes

,

shortcuts and tricks

,

MCQs

,

Extra Questions

,

Summary

,

Important questions

,

Previous Year Questions with Solutions

,

mock tests for examination

,

SAP ABAP - Encapsulation | Learn and Master SAP ABAP - Software Development

,

Free

,

past year papers

,

SAP ABAP - Encapsulation | Learn and Master SAP ABAP - Software Development

,

ppt

,

study material

,

practice quizzes

,

video lectures

,

Viva Questions

;