Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

constructor in an Abstract class

Former Member
0 Kudos
2,079

Can we have a constructor in an Abstract class??

7 REPLIES 7

Former Member
0 Kudos
642

Hello,

We can't have a constructor method in abstract classe, take a look at this: .

Regards,

matt
Active Contributor
0 Kudos
642

You might be able to achieve your functionality by using a concrete class and interfaces. It depends what you're trying to achieve.

0 Kudos
642

Hello Harshu

Of course you can have a CONSTRUCTOR within an abstract class. Such a CONSTRUCTOR method can be used to preset values for inherited attributes. Obviously if we can have a CONSTRUCTOR method we can put any kind of implementation into it.

For abstract class we are not able to create object.

We can inherit that constructor and then we can call in child class constructor.

CONSTRUCTOR methods are the only public methods which are NOT inherited. It is wrong to say that we can call the CONSTRUCTOR in sub-classes but we MUST call super->CONSTRUCTOR otherwise we get a syntax error.

Have a look at sample report ZUS_SDN_ABSTRACT_CLASS (the same logic holds for global classes):


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ABSTRACT_CLASS
*&
*&---------------------------------------------------------------------*
*& Thread: constructor in an Abstract class
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="862886"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_abstract_class.


*----------------------------------------------------------------------*
*       CLASS lcl_abstract DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_abstract DEFINITION ABSTRACT.

  PUBLIC SECTION.

    METHODS: constructor.

  PROTECTED SECTION.
    DATA: md_counter  TYPE i.
ENDCLASS.                    "lcl_abstract DEFINITION



*----------------------------------------------------------------------*
*       CLASS lcl_abstract IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_abstract IMPLEMENTATION.

  METHOD constructor.
    WRITE: / 'CONSTRUCTOR of abstract class'.

    md_counter = 5. " preset counter with value
  ENDMETHOD.                    "constructor


ENDCLASS.                    "lcl_abstract IMPLEMENTATION



*----------------------------------------------------------------------*
*       CLASS lcl_subclass DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass DEFINITION INHERITING FROM lcl_abstract.

  PUBLIC SECTION.

    METHODS: constructor,
             get_counter.


ENDCLASS.                    "lcl_subclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_subclass IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass IMPLEMENTATION.

  METHOD constructor.
    super->constructor( ).  " syntax error if not called
    WRITE: / 'CONSTRUCTOR of sub-class'.
  ENDMETHOD.                    "constructor

  METHOD get_counter.
    WRITE: / 'Counter =', me->md_counter.
  ENDMETHOD.                    "get_counter

ENDCLASS.                    "lcl_subclass IMPLEMENTATION


DATA: go_subclass   TYPE REF TO lcl_subclass.


START-OF-SELECTION.

  CREATE OBJECT go_subclass.
  SKIP.
  go_subclass->get_counter( ).

" OUTPUT:
" CONSTRUCTOR of abstract class
" CONSTRUCTOR of sub-class
"
" Counter =          5

END-OF-SELECTION.

Regards

Uwe

Former Member
0 Kudos
642

Hi,

Theoretically we can create a constructor in abstract class.

But we do not have implementation in that.

constructor will be called when we are creating object.

For abstract class we are not able to create object.

We can inherit that constructor and then we can call in child class constructor.

Regards,

sarath

Former Member
0 Kudos
642

Hi,

refer to this example

REPORT zsr_demo1.

CLASS c1 DEFINITION ABSTRACT.

public section.

methods constructor.

PROTECTED SECTION.

METHODS m1.

PRIVATE SECTION.

DATA a1 TYPE string VALUE `Attribute A1 of class C1`.

ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1 FINAL.

PUBLIC SECTION.

METHODS m2.

ENDCLASS.

CLASS c1 IMPLEMENTATION.

method constructor.

write:'This is my first method'.

endmethod.

METHOD m1.

WRITE / a1.

ENDMETHOD.

ENDCLASS.

CLASS c2 IMPLEMENTATION.

METHOD m2.

m1( ).

ENDMETHOD.

ENDCLASS.

DATA oref TYPE REF TO c2.

START-OF-SELECTION.

CREATE OBJECT oref.

oref->m2( ).

Former Member
0 Kudos
642

Hi,

How can we have a constructor for an abstract class??

First of all you can not create a object of an abstract class then how can we have a constructor for an abstract class and what is the need to have an constructor for an abstract class.

Regards,

Naresh.

matt
Active Contributor
0 Kudos
642

You can't instantiate an abstract class but you can have a constructor. It is only ever called, however, when instantiating an object of a subclass. ( Indeed, it MUST be called in the most recent releases ).