Monday, March 25, 2013

Abstract Class cannot be Instantiated

Abstract class is something which contains at least one abstract method. Now abstract method is not implemented at the implementation of abstract class. To implement that abstract method we have to create a subclass of the abstract class and then we can implement it.

We cannot create any object (instance) for an abstract class. If we instantiate then syntax error will come. We can call normal methods defined in abstract class inside the subclass (child class). Now by instantiating the subclass we can have normal methods of abstract class.

 
·         In the following program we define an abstract class cl_abs.
·         We have a normal method m_normal and an abstract method m_abs.
·         At the implementation part we can implement normal methods only.
·         Now we define a subclass cl_normal of the abstract class cl_abs.
·         There we define a normal method m_child and redefine the abstract method m_abs.
·         Since we already have defined the abstract method m_abs, we have to redefine it.
·         At the implementation part of this subclass we can implement the abstract method m_abs.
·         Here we can call the normal method of abstract class.
·         Now at the start of selection we only can create object for the subclass because abstract class cannot be instantiated.

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 in Concrete Class
*----------------------------------------------------------------------*
CLASS cl_normal IMPLEMENTATION.
  METHOD m_abs. "Previously defined Abstract Method
    WRITE: /3 'Abstract method from child of Abstract class'.
    SKIP.
  ENDMETHOD.                    "m_abs

  METHOD m_child.
    CALL METHOD m_normal.
    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.

1 comment:

Anonymous 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!!!!!!