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.

7 comments:

Ramya k said...

Thanks for the great information, was looking for this information from long.Great blogSAP MM Training in Chennai

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

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD