Skip to Content

Abstract Class and class-method

Hi,

I have been reading various posts on OOPS ABAP and below is the inference i have got so far.

We go for abstract classes when we have some common functions and some unique functions respective to implementing class.

Interfaces can also achieve what is mentioned above about abstract classes but we mainly go for it for polymorphism and when there is no need for common functions/global functions between implementing classes

My question is ?? what i told about abstract class can also be achieved using class-methods, class-data (for global functions) and normal class inheritance (for redefining unique functions), then why should we go for abstract classes.

Moreover, we have a disadvantage with abstract classes, that all the implementing classes must redefine all the abstract methods. We add a new abstract method to an abstract class, we need to define it in all implementing classes.

Then why are we not sticking with using class-method and normal inheritance and instead go for abstract classes?

Even interfaces have this disadvantage, but it is the only way for polymorphism.

Also, the global/common methods of an abstract class can be redefined where as class-methods cannot be, helping us achieve OO Pardigm better.

Could anyone please help me understand this, please forgive me if i am wrong anywhere.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jul 19, 2016 at 06:41 PM

    Hello All,

    Thank you so much, the below try out was what actually made me doubt why and when do we really need abstract class ( programming wise, not conceptual )

    A scenario

    abstract_class

    method display importing info FINAL "common method

    abstract method "get info" "method for redefinition

    "if we add new abstract method we need to redefine in all classes - Issue

    class1 inheriting from abstract_class

    redefine method "get info" to "get name info"

    class2 inheriting from abstract_class

    redefine method "get info" to "get age info"

    class_trigger

    class-method call_all importing classimp type ref to abstract_class

    call method classimp->get_info()

    call method classimp->display()

    endmethod

    create objects class1, class2

    class_trigger->call_all ( class1 ).

    class_trigger->call_all ( class2 ).

    Now, I can do the same using class-method and normal redefinition

    Main_class

    class-method display importing info "common method

    method "get info" ""method for redefinition

    class1 inheriting from main_class

    redefine method "get info" to "get name info"

    class2 inheriting from main_class

    redefine method "get info" to "get age info"

    class_trigger

    class-method call_all importing classimp type ref to abstract_main

    call method classimp->get_info()

    call method classimp->display()

    endmethod

    create objects class1, class2

    class_trigger->call_all ( class1 ).

    class_trigger->call_all ( class2 ).

    i think that's what i am trying to know, why go through the trouble of creating abstract classes methods and compel implementing classes to redefine them all.

    @Frank kirck , could you please help me understand this scenario and why abstract is needed

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 19, 2016 at 05:48 PM

    If you have a superclass with 10 methods "concrete" and 15 methods abstract, and you have subclasses where you only implement these abstract methods and need not reimplementing the 10 concrete methods, would you really instead prefer not using an abstract class and implementing the same code in all 10 methods of all subclasses? I prefer doing the code once.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 19, 2016 at 03:25 PM

    Puhhh, where to start?

    In the usual case you use abstract classes i.e. abstract methods only for these cases, where you really want to redefine it always in the sub classes. It's not needed to define all methods as abstract, you can implement methods also in an abstract class.

    Of course you can also avoid a redefinition of an already implemented method by using FINAL.

    Interfaces are good to hide implementations (open-close-principle) and to seperate concerns. They are also very good for dependency injections in well layered applications.

    If you say, you can implement the functionalities also as static methods, then it sounds like you need in fact helper or utility classes. So you should not take inheritance as solution, but delegation.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.