Monday, March 25, 2013

Abstract Method


Abstract method can only be implemented in any of sub classes of the parent class by using REDEFINITION of the method. If we try to implement any abstract method then syntax error will come as follows:


We can implement the abstract method in any child or sub class by defining the method with REDEFINITION. Below is the same program but we have implemented the abstract method in the child class inherited from the parent. Before implementation we have defined the method by REDEFINITION. In this way we can use an abstract method in any program.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_abs DEFINITION
*----------------------------------------------------------------------*
*       This Abstract class contains abstract & normal methods
*----------------------------------------------------------------------*
CLASS cl_abs DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS: m_normal,
             m_abs ABSTRACT. "It will not be implemented
ENDCLASS.                    "cl_abs DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_abs IMPLEMENTATION
*----------------------------------------------------------------------*
*       Only the normal method will be implemented here
*----------------------------------------------------------------------*
CLASS cl_abs IMPLEMENTATION.
  METHOD m_normal.
    WRITE: /3 'Normal Method in Abstract class'.
  ENDMETHOD.                    "m_normal
ENDCLASS.                    "cl_abs IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_normal DEFINITION - Concrete Class
*----------------------------------------------------------------------*
*       Creating child class to implement the abstract method
*----------------------------------------------------------------------*
CLASS cl_normal DEFINITION INHERITING FROM cl_abs.
  PUBLIC SECTION.
    METHODS: m_abs REDEFINITION, "Since it has already been declared
             m_child.
ENDCLASS.                    "cl_normal DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_normal IMPLEMENTATION
*----------------------------------------------------------------------*
*       The predefined abstract method is implemented here
*----------------------------------------------------------------------*
CLASS cl_normal IMPLEMENTATION.
  METHOD m_abs. "Previously defined Abstract Method
    WRITE: /3 'Abstract method from child of Abstract class'.
    CALL METHOD m_normal.
    SKIP.
  ENDMETHOD.                    "m_abs

  METHOD m_child.
    WRITE: /3 'Normal child method'.
  ENDMETHOD.                    "m_child
ENDCLASS.                    "cl_normal IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_normal TYPE REF TO cl_normal.

  CREATE OBJECT obj_normal.       "Instantiating the Concrete class
  CALL METHOD: obj_normal->m_abs,
               obj_normal->m_child.

Below is the Output:

6 comments:

yektek training said...

nice post thank you

simashree said...

Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

sanayamohit said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

suganya said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!