Ads

Abstract Class and Interface in SAP ABAP

Abstract Class and Interface in SAP ABAP

When the SAP world increasing ,It have to compete with other software packages .In that case ,Object oriented Programming is the most important language to increase the reuse ability of ABAP functions.Abstract classes and interfaces are also part of OO ABAP concepts .So every ABAP consultant Must be know clear basics of OOPs Concepts.Today ,we will discuss ,what is Abstract class and interfaces and difference between them.

What is an Abstract Class?

Abstract Class is also like a class but it has special features and it can’t be instantiated itself .It means we can't execute abstract class itself but we can execute it from the inherited classes that is we can use components(methods) of Abstract class in the inherited classes . Abstract class should at least contain one abstract method. Abstract methods are methods without any implementation – only a declaration. We can certainly define the variables referencing to Abstract class and instantiate with specific subclass at run time.

Abstract Class which contain one or more abstract methods which does't have implementation. These abstract methods or properties are implemented in the derived classes (Sub-classes).  
Abstract classes does not create any instances to that class objects.The main advantage of Abstract class ,when we need common functionalities ,Abstract class used as Super class which common functionalities can be used in inherited(child class) classes.  

Abstract Class syntax

Abstract class

CLASS ZCL_ABAP_TUTOR DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS: NUMBER ABSTRACT IMPORTING iv_NUMBER TYPE STRING .
    METHODS: WRITE_NUMBER.
  PRIVATE SECTION.
    DATA: number TYPE STRING.
ENDCLASS.                    

Subclass 

CLASS zcl_sub_class IMPLEMENTATION.
  METHOD WRITE_NUMBER.
  ENDMETHOD.                    
ENDCLASS.                    



What are an Interfaces?

Interface definition contains only declaration of all components which are attributes, methods, events of the interface.The components of interfaces do not have to be assigned individually to a visibility section(Public,private,protect), so they automatically belong to the public section of the class in which the interface is implemented. Interfaces does't have an implementation part,Instead of it we are implementing the methods  of interface in the derived class .

Like classes, we can define interfaces either globally in the Repository or locally in an ABAP program. 

INTERFACE intf.
...
ENDINTERFACE.

Implementing Interfaces

Unlike classes, interfaces do not have instances. So, interfaces are implemented by classes. To implement an interface in a class, use the statement
INTERFACES intf.

METHOD intf~imeth.
...
ENDMETHOD.


How to declare Interface References

Reference variables allow you to access objects (refer to Working with Objects). Instead of creating reference variables with reference to a class, you can also define them with reference to an interface. 

To define an interface reference, use the addition TYPE REF TO intf in the TYPES or DATA statement. 


Calling Objects Using Interface References

If a class implements an interface intf, you can use the following assignment between the class reference variable cref and an interface reference iref to make the interface reference in iref point to the same object as the class reference in cref:
iref = cref

If the interface intf contains an instance attribute attr and an instance method meth, you can call the interface components as follows:

Using the class reference variable cref:
•        To access an attribute attr: cref->intf~attr
•        To call a method meth: CALL METHOD cref->intf~meth

Using the interface reference variable iref:
•        To access an attribute attr: iref->attr
•        To call a method meth: CALL METHOD iref->meth


To access a constant const: intf=>const

To access a static attribute attr: class=>intf~attr

To call a static method meth: CALL METHOD class=>intf~meth

Nesting Interfaces

 An interface which contains another interface is called a nested or a compound interface. An interface which is contained in another interface is referred to as the component interface. 

INTERFACE i3.
  ...
  INTERFACES: i1, i2 ...
  ...
ENDINTERFACE.

Here, the interface i3 consists of its components as well as of the interfaces i1 and i2. 

Using Alias Names

You can use the ALIAS statement in interface definitions to assign alias names to the components of component interfaces. This makes these components visible in the interface definition.

INTERFACE i2.
  ...
  INTERFACES i1.
  ALIASES alias21 FOR i1~comp1.
  ...
ENDINTERFACE.
INTERFACE i3.
  ...
  INTERFACES i2.
  ALIASES alias31 FOR i2~alias21.
  ALIASES alias32 FOR i2~comp2.
  ...
ENDINTERFACE.

Assigning Interface References

You can assign interface references typed with reference to one of the component interfaces to interface references typed with reference to a nested interface. You can then use the interface references typed with reference to a component interface to address the components of the component interfaces.

INTERFACE i2.
  ...
  INTERFACES i1.
  ...
ENDINTERFACE.

INTERFACE i3.
  ...
    INTERFACES i2.
  ...
ENDINTERFACE.


iref2 = iref3.
iref1 = iref2.

Implementing Nested Interfaces in Classes

When you implement a nested interface in a class, then all interfaces contained are implemented in the class at the same level, whatever their nesting depth. The class must then implement all methods.
INTERFACE i2.
  ...
  INTERFACES i1.
  ...
ENDINTERFACE.
INTERFACE i3.
  ...
  INTERFACES i2.
  ...
ENDINTERFACE.
CLASS class DEFINITION.
  ...
  INTERFACES i3.
  ...
ENDCLASS.
CLASS class IMPLEMENTATION.
  ...
  METHOD i1~meth.
    ...
  ENDMETHOD.
  ...
ENDCLASS.


Difference between Abstract Class and Interfaces in OO ABAP 


Abstract ClassInterfaces
Not possibleMultiple inheritence possible
It is not require in this case becuase there is no need to redefine Non-abstract methods in subclassesAll inherited methods must be implement in derived classes
We can define a default function of a non-abstract method in abstract classWe can't define any implementation to interfaces
we can assign the components of the Abstract class to visibility sectionAll interfaace components are public by defualt

Comments

Post a Comment

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced