Application Development and Automation 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: 
Read only

Calling a method from abstarct class

Former Member
0 Likes
2,893

Hi Experts,

Am working on ABAP Objects.

I have created an Abstract class - with method m1.

I have implemented m1.

As we can not instantiate an abstract class, i tried to call the method m1 directly with class name.

But it it giving error.

Please find the code below.

CLASS c1 DEFINITION ABSTRACT. 
  PUBLIC SECTION. 
    DATA: v1 TYPE i. 
    METHODS: m1. 
ENDCLASS.                    "c1 DEFINITION 

CLASS c1 IMPLEMENTATION. 
  METHOD m1. 
    WRITE: 'You called method m1 in class c1'. 
  ENDMETHOD. "m1 
ENDCLASS.                    "c1 IMPLEMENTATION 

CALL METHOD c1=>m1.

Please tell me what is wrong and how to solve this problem.

Thanks in Advance.

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
1,309

Micky is right, abstract means not to be instantiated. It is just a "template" which you can use for all subsequent classes. I.e you have general abstract class vehicle . For all vehicles you will have the same attributes like speed , engine type , strearing , gears etc and methods like start , move etc.

In all subsequent classes (which inherit from vehicle) you will have more specific attributes for each. But all of these classes have some common things (like the ones mentioned above), so they use abstract class to define these things for all of them.

Moreover there is no sense in creating instance (real object) of class vehicle . What kind of physical object would vehicle be? there is no such object in real world, right? For this we need to be more precise, so we create classes which use this "plan" for real vehicles. So the abstract class here is only to have this common properties and behaviour defined in one place for all objects which will have these. Abstract object however cannot be created per se. You can only create objects which are lower in hierarchy (which are specific like car , ship, bike etc).

Hope this claryfies what we need abstract classes for.

Regards

Marcin

3 REPLIES 3
Read only

Sm1tje
Active Contributor
0 Likes
1,309

Well Mytri, actually this is something that can be found in the ABAP (keyword) help. Just enter abstract in your search (from your program) and you will see the following:

The ABSTRACT addition defines an abstract class class. You cannot create object instances from an abstract class. To use the instance components of an abstract class, a concrete subclass of the class must be instantiated.

In other words, you will have to create a second class inheriting from the abstract class, instantiate the second class and call method m1 from abstract class.

Read only

MarcinPciak
Active Contributor
0 Likes
1,310

Micky is right, abstract means not to be instantiated. It is just a "template" which you can use for all subsequent classes. I.e you have general abstract class vehicle . For all vehicles you will have the same attributes like speed , engine type , strearing , gears etc and methods like start , move etc.

In all subsequent classes (which inherit from vehicle) you will have more specific attributes for each. But all of these classes have some common things (like the ones mentioned above), so they use abstract class to define these things for all of them.

Moreover there is no sense in creating instance (real object) of class vehicle . What kind of physical object would vehicle be? there is no such object in real world, right? For this we need to be more precise, so we create classes which use this "plan" for real vehicles. So the abstract class here is only to have this common properties and behaviour defined in one place for all objects which will have these. Abstract object however cannot be created per se. You can only create objects which are lower in hierarchy (which are specific like car , ship, bike etc).

Hope this claryfies what we need abstract classes for.

Regards

Marcin

Read only

Former Member
0 Likes
1,309

Hi Mytri,

In an abstract class there should be a method which is abstract( ie without having any implementation )

How your class is syntactically corrrect I dont know.

These abstract methods needs to be implemented/redefined in the subclasses .

For eg think of an class animal ( which is an ideal example of Abstract class ) which is having an abstract method eat( )

. You need to have subclasses like Lion Tiger etc where we can clearly redefine/implement this method