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

Working with Abstract Class

Former Member
0 Likes
1,241

I created a class by declaring it as Abstract.I created a sub class of the Abstract class(Inherting by giving the name of the super clasS) and when I try to activate it says Method not declared or Inherited in Sub class .The Abstract class is inactive and when i Activate it says method is abstract

I have inherited the method by going to the Go to Method definition.The class is also inherited.I am unsure where i have gone wrong.

Please help me understand where I have gone wrong.

7 REPLIES 7
Read only

MarcinPciak
Active Contributor
0 Likes
1,199

If the method is declared as abstract you have to redefine it in your subclass.

If, in contrary, the method is not abstract, you need to provide its implementation in an abstract class.

Regards

Marcin

Read only

0 Likes
1,199

I had created a class by defining it as abstract.I have a method say m1.Since the class is abstract,I will not be able to implement the method m1 of the abstract class.

I had created a sub class by inheriting from the abstract class.I am trying to implement the method of the abstract class.

When I try to activate the sub class,it says Method not declared or Inherited in Sub class .

This is what I have declared it in the method.

  • Local Data Declarations

DATA: lv_count TYPE i,

lv_res TYPE i.

  • initialize Count value to '1'

lv_count = '1'.

DO 10 TIMES.

IF lv_count <= '10'.

lv_res = v_num * lv_count.

  • Displa the multiplication table for a Given Number

WRITE: / v_num,

'*',

lv_count,

'=',

lv_res.

  • Increment Count value

lv_count = lv_count + 1.

ELSE.

EXIT.

ENDIF.

ENDDO.

  • Clear variable

CLEAR: v_num.

endmethod

Correct me where I have gone wrong or if I have missed something...

Edited by: Viswanathan_1983 on Jan 7, 2010 2:03 PM

Read only

0 Likes
1,199

I had created a class by defining it as abstract.I have a method say m1.Since the class is abstract,I will not be able to implement the method m1 of the abstract class.

Abstract for class means you cannot instiante it. You can however implement method inside it, unless you declared the method with ABSTRACT addition.

I had created a sub class by inheriting from the abstract class.I am trying to implement the method of the abstract class.

Either:

- change the method definition in abstract class to be also abstract -> then redefine it in subclass. This way you have implementation provided in subclass

or

- implement the method in abstract class and use it in your subclass (or redefine to extend the functionality in subclass)

This should help

Regards

Marcin

Read only

0 Likes
1,199

Thats what I did first(Sorry I did not explain properly earlier) but not working..

I had created a class by defining it as abstract.I have a method say m1 that is declared as abstract.Since the class is abstract,I will not be able to implement the method m1 in the abstract class.When I activate the abstract class,it says method cannot be implemented (which is true)but would we not be able to activate ther abstract class??

I had created a sub class by inheriting from the abstract class ie the base class here.I am trying to implement the method m1 in the sub class.

When I try to activate the sub class,it says Method not declared or Inherited in Sub class .I am sure I gave the Abstract class name to inherit it.

This is what I have declared it in the method.

method m1.

  • Local Data Declarations

DATA: lv_count TYPE i,

lv_res TYPE i.

  • initialize Count value to '1'

lv_count = '1'.

DO 10 TIMES.

IF lv_count <= '10'.

lv_res = v_num * lv_count.

  • Displa the multiplication table for a Given Number

WRITE: / v_num,

'*',

lv_count,

'=',

lv_res.

  • Increment Count value

lv_count = lv_count + 1.

ELSE.

EXIT.

ENDIF.

ENDDO.

  • Clear variable

CLEAR: v_num.

endmethod.

Correct me where I have gone wrong or if I have missed something...Pls give me ur mail id so that I can explain line by line what I did..

Edited by: Viswanathan_1983 on Jan 7, 2010 2:03 PM

Edited by: Viswanathan_1983 on Jan 7, 2010 2:11 PM

Edited by: Viswanathan_1983 on Jan 7, 2010 2:14 PM

Edited by: Viswanathan_1983 on Jan 7, 2010 2:14 PM

Edited by: Viswanathan_1983 on Jan 7, 2010 2:16 PM

Edited by: Viswanathan_1983 on Jan 7, 2010 2:17 PM

Read only

0 Likes
1,199

I think the problem is that you didn't add REDEFINITION addition to method m1 in subclass. See below snippet


CLASS lcl_abstract DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS m1 ABSTRACT.
ENDCLASS.                   

CLASS lcl_subclass DEFINITION INHERITING FROM lcl_abstract.
  PUBLIC SECTION.
    METHODS m1 REDEFINITION.  "this one you are missing I guess
ENDCLASS.                    

CLASS lcl_subclass IMPLEMENTATION.
  METHOD m1.
    WRITE 'Method m1 implemented in sublcass'.
  ENDMETHOD.                 

ENDCLASS.                   

START-OF-SELECTION.
  DATA r_sub TYPE REF TO lcl_subclass.

  CREATE OBJECT r_sub.
  r_sub->m1( ).

Regards

Marcin

Read only

0 Likes
1,199

Marcin and All,

I followed the document from a website called Saptechnical and under the tab Object Oriented Programming. Pls correct me If I have missed something or of something is missing here..

The steps I followed for Abstract Class are below.

1) Transaction Se24 created a class with Instantiation as Abstract.

2) I defined an atribute X as Instance attibute and visibility as public.

3) I defined an Method M1 as a instance Method and Visibiity as protected.

4) I went inside the method and selected Go to Tab -> Method definition and selected abstract.By this method is also an abstract.When I chose abstract,I got a pop up message stating Implementation of the method M1 deleted.

5) I then clicked on continue and a pop up messgae "Method changed sucessfully".

This is where when i try to activate the class ,I get a message abstract class cannot be implemented(I understand that the abstract methods placed inside the abstract class cannot be implemented but is it not possible to activate the class)

The steps I followed for Sub Class are below.

1) Transaction Se24 created a sub class and gace the Abstract class for inhering the properties of the abstract class.

2 ) The Method that was declared in Abstract class automatically came in Sub Class.

3) I selected the method and clicked on the redefinition method.

4) Between Method and End Method i wrote now a very simple logic.

method m1.

DATA: Var1 TYPE I.

VAR1 = X + 1.

Write : / VAR1.

endmethod.

5) I then saved the class and activated the class.When I try to activate ,I get the message the method not implemented or declared.This is all I did.I have not declared the object like u had shared.Can you letme know what next steps I should be doing.

Your help is immensely appreciated...

You had shared the following code with me.

CLASS lcl_abstract DEFINITION ABSTRACT.

PUBLIC SECTION.

METHODS m1 ABSTRACT. " Already done as I explained in my Demo for Abstract class Correct me if I am wrong.."

ENDCLASS.

CLASS lcl_subclass DEFINITION INHERITING FROM lcl_abstract.

PUBLIC SECTION.

METHODS m1 REDEFINITION. "this one you are missing I guess " Already done as I explained in my Demo for Sub class Correct me if I am wrong.."

ENDCLASS.

CLASS lcl_subclass IMPLEMENTATION.

METHOD m1.

WRITE 'Method m1 implemented in sublcass'. " Already done as I explained in my Demo for Sub class .Correct me if I am wrong"

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA r_sub TYPE REF TO lcl_subclass. I have not created a program to declare the object.Can you let me know the steps to take this forward.

CREATE OBJECT r_sub.

r_sub->m1( ).

Edited by: Viswanathan_1983 on Jan 8, 2010 7:11 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:37 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:41 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:43 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:44 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:45 AM

Edited by: Viswanathan_1983 on Jan 8, 2010 7:45 AM

Read only

0 Likes
1,199

Thanks Marcin for all your help.My query is resolved now..What i did was I had declared the method as protected in the base class and was trying to redefine in Sub class as public and hence there was a conflict .I changed to Public in the Abstract class and now it works absolutely fine..

Thanks once again for all your help.