Static Interface Methods in ABAP

Hello,
I'd like to know what sense it makes to define static methods in an interface.
My understanding is, that an interface can only be used with an instance of a class implementing that interface. So I always need to have an instance and thus, static methods are not needed.
But ABAP-OO (in contrast to Java, for example) allows that, so maybe there is some need for that?
Thanks for your help, best regards,
Timo Wolf.

Hi,
As you know that static methods are not instance- specific. Hence when a class implements an interface, the methods defined as static do not require an instance-specific call. Rather they are called as CLASSNAME=>INTREFACE_NAME~STATIC_METHOD_NAME. You do not need an instance to access them.
The only purpose of declaring a method as STATIC in interface definition is to create an agreement that a particular method will not depend on an instance to work. When a class implements the interface, calls to this method will be as mentioned above.
<b>
As far as the static components of interfaces are concerned, you can only use the interface name to access constants:
Addressing a constant <const>: <intf>=><const>
For all other static components of an interface, you can only use object references or the class <class> that implements the interface:
Addressing a static attribute <attr>: <class>=><intf~attr>
Calling a static method <meth>: CALL METHOD <class>=><intf~meth>
</b>
Hope this helps.
Regards
Message was edited by: Shehryar Khan
Message was edited by: Shehryar Khan

Similar Messages

  • Apply static method requirements on class (Static interface methods)?

    I have a set of classes where is class is identified with a unique ID number. So each class has a public static int getId() method. Each class also has either a constructor that takes a byte array or a static factory method that takes a byte array.
    I would like to maintain a hashmap of these classes keyed to the ID's however I am not sure how to have some interface of abstract parent class that requires each of these classes to implement these static methods.
    What I was hoping to do was something like this>>>
         public interface MyClasses{
              static MyClasses createInstance(byte[] data);
         HashMap<Integer, MyClasses> map;
         public void init() {
              map = new HashMap<Integer, MyClasses>();
              map.put(Class1.getId(), Class1.class);
              map.put(Class2.getId(), Class2.class);
         public void createInstance (int id, byte[] data) {
              map.get(id).createInstance(data);
         }Is there some way I could do this?

    What about something like this?
    public interface Initializable
         public void initialize(byte[] data);
    public class Factory
         private final Map<Integer, Initializable> map = new HashMap<Integer, Initializable>();
            public void registerClass(int id, Class klass)
                    if (! Initializable.class.isAssignableFrom(klass))
                             // you may also want to ensure the class declares a parameterless constructor
                             throw new IllegalArgumentException("duff class");
                    if (this.map.keySet().contains(id))
                             throw new IllegalArgumentException("duff id");
              this.map.put(id, klass);
         public Initializable createInstance(int id, byte[] data)
                    // need some exception handling of course
              Initializable i = map.get(id).newInstance();
                    i.initialize(data);
                    return i;
    }

  • Calling an interface METHOD of another abap web dynpro application

    Hi Experts,
    Can u plz tell how we can Call an interface METHOD of another abap web dynpro application in main WD Component.
    Thanks
    Mahesh

    Hi ,,
       Example ALV interface method calling   GET_MODEL interface method
       View attribute   declaration   :    M_WD_ALV  type      IWCI_SALV_WD_TABLE
         DATA lo_INTERFACECONTROLLER TYPE REF TO IWCI_SALV_WD_TABLE .
          wd_this->M_WD_ALV =   wd_this->wd_cpifc_alv( ).   "ALV is the usage name
         DATA lv_value TYPE ref to cl_salv_wd_config_table.
          lv_value = wd_this->M_WD_ALV->get_model(  ).   " interface method calling in ALV component usage.....
    Regards,
    Devi

  • Redefine Static Methods in ABAP OO

    Hello,
    I want to redefine an public static method and returns always an error.
    Okay, I already solved the problem with an workaround, but I still don't understand, why it is not possible to redefine static methods in ABAP OO.
    If someone can give me an plausible reason, so I don't have do die stupid. G
    Thanks for help!
    Matthias

    It is built into the language that way.  HEre is a link that may or may not give you an answer.
    redefine static method?
    Regards,
    Rich Heilman

  • Regarding interface in the ABAP-HR

    Hi Experts,
             Iam new to ABAP-HR,
             I have 2 custom table which are updated with OT records from one batch program.
             Now i have to generate two files for regular OT data file and Off-Cycle OT data file respectually.
    What i have to do?For this i have to  pick the data from those two custom tables and create the files by using open dataset statement,Is this right way in HR?
    Actually the selection screen is having Payroll area,Current period and Other period fields.
    and the table is having BEGDA,ENDDA in one table and OTdate in onother table.How can i compare the dates?

    Hi,
    <b>Interface</b> Can be declared globally or locally within a program.
    Locally declared in the global portion of a program using:-
    INTERFACE <intf>.
    ENDINTERFACE.
    The definition contains the declaration for all components (attributes, methods, events) of the interface.
    Interfaces are included in the public section of a class.
    Interfaces do not have an implementation part, since their methods are implemented in the class that implements the interface.
    Interfaces do not have instances.
    A component <icomp> of an interface <intf> can be addressed as though it were a member of the class under the name <intf~icomp>.
    <b>Addressing Objects Using the class reference variable <cref>:</b>
    To access an attribute <attr>: <cref>-><intf~attr>
    To call a method <meth>:
    CALL METHOD <cref>-><intf~meth>
    <b>Using the interface reference variable <iref>:</b>
    To access an attribute <attr>: < iref>-><attr>
    To call a method <meth>:
    CALL METHOD <iref>-><meth>
    Addressing a constant <const>:
    <intf>=><const> (Cannot use class name).
    Addressing a static attribute <attr>:
    < class>=><intf~attr>
    Calling a static method <meth>:
    CALL METHOD <class>=><intf~meth>
        (Cannot use Interface method ).
    casting operator (?= )
    <cref> ?= <iref>
         For the casting to be successful, the object to which <iref> points must be an object of the same class as the type of the class variable <cref>.
    <b>sample program</b>
    report ysubdel .
    interface i1.
    data    : num type i .
    methods : meth1.
    endinterface.
    class c1 definition.
      public section.
      methods : meth1.
      interfaces : i1.
    endclass.
    class c1 implementation.
      method : meth1.
       write:/5 'I am meth1 in c1'.
      endmethod.
      method i1~meth1.
       write:/5 'I am meth1 from i1'.
      endmethod.
    endclass.
    start-of-selection.
      data : oref type ref to c1.  create object oref.
      write:/5 oref->i1~num.
      call method oref->meth1.
      call method oref->i1~meth1.
    Regards,
    Sowjanya.

  • Class/Interface/Methods

    Hi All,
    I'm trying to understand the concept behind abap class/interface/methods.
    1. Is it mandatory to have an interface in every class?
    2. Once a class is created is it right to define methods as static and public and use them without reference to the interface?
    Any help appreciated.
    Meghna

    hi meghna,
    interfaces in ABAP Objects play the same role as classes. Just like classes, interfaces are object types that reside in the namespace of all types. While a class describes all aspects of a class, an interface only describes a partial aspect
    The syntax for declaring a local interface is:
    INTERFACE intf.
       DATA ...
       CLASS-DATA ...
       METHODS ...
       CLASS-METHODS ...
    ENDINTERFACE.
    Basically, the declaration of an interface corresponds to the declaration part of a class, where instead of CLASSu2014ENDCLASS, you simply use INTERFACEu2014ENDINTERFACE. Interfaces can contain exactly the same components as classes. Unlike classes, however, interfaces don't need to be divided into different visibility sections because interface components are always integrated in the public visibility section of classes.
    i dont want to confuse you .
    please go to the below link to know more about... interface and classes.
    [http://help.sap.com/saphelp_nw04/helpdata/en/ec/d9ab291b0b11d295400000e8353423/frameset.htm]

  • What is interface in oops abap

    hi experts.
    what is interface in oops abap.
    thanks.
    subhasis

    Like classes, you can define interfaces either globally in the R/3 Repository or locally in an ABAP program. For information about how to define local interfaces, refer to the  Class Builder section of the ABAP Workbench Tools documentation. The definition of a local interface <intf> is enclosed in the statements:
    INTERFACE <intf>.
    ENDINTERFACE.
    The definition contains the declaration for all components (attributes, methods, events) of the interface. You can define the same components in an interface as in a class. The components of interfaces do not have to be assigned individually to a visibility section, since they automatically belong to the public section of the class in which the interface is implemented. Interfaces do not have an implementation part, since their methods are implemented in the class that implements the interface.
    <b>Implementing Interfaces</b>
    Unlike classes, interfaces do not have instances. Instead, interfaces are implemented by classes. To implement an interface in a class, use the statement
    INTERFACES <intf>.
    in the declaration part of the class. This statement may only appear in the public section of the class.
    When you implement an interface in a class, the components of the interface are added to the other components in the public section. A component <icomp> of an interface <intf> can be addressed as though it were a member of the class under the name <intf~icomp>.
    The class must implement the methods of all interfaces implemented in it. The implementation part of the class must contain a method implementation for each interface method <imeth>:
    METHOD <intf~imeth>.
    ENDMETHOD.
    Interfaces can be implemented by different classes. Each of these classes is extended by the same set of components. However, the methods of the interface can be implemented differently in each class.
    Interfaces allow you to use different classes in a uniform way using interface references (polymorphism). For example, interfaces that are implemented in different classes extend the public scope of each class by the same set of components. If a class does not have any class-specific public components, the interfaces define the entire public face of the class.
    <b>
    Addressing Objects Using Interface References</b>
    To create an object of the class <class>, you must first have declared a reference variable <cref> with reference to the class. If the class <class> implements an interface <intf>, you can use the following assignment between the class reference variable <cref> and an interface reference <iref> to make the interface reference in <iref> point to the same object as the class reference in <cref>:
    <iref> = <cref>
    If the interface <intf> contains an instance attribute <attr> and an instance method <meth>, you can address the interface components as follows:
    Using the class reference variable <cref>:
    To access an attribute <attr>: <cref>-><intf~attr>
    To call a method <meth>: CALL METHOD <cref>-><intf~meth>
    Using the interface reference variable <iref>:
    To access an attribute <attr>: < iref>-><attr>
    To call a method <meth>: CALL METHOD <iref>-><meth>
    As far as the static components of interfaces are concerned, you can only use the interface name to access constants:
    Addressing a constant <const>: < intf>=><const>
    For all other static components of an interface, you can only use object references or the class <class> that implements the interface:
    Addressing a static attribute <attr>: < class>=><intf~attr>
    Calling a static method <meth>: CALL METHOD <class>=><intf~meth>

  • Meaning of factory method in abap objects

    Hi,
    Can anybody help me in understanding the meaning of factory method in abap object? what is the importance of this?
    Regards
    Sudhansu

    Hi Krish and Sudhansu,
    Design patterns are solutions which are already verified and known by many developers. That is why it is worth to use them. There is no need to reinvent the wheel in many cases.
    I would recommend book which is placed in the ABAP world:
    http://www.sap-press.com/products/Design-Patterns-in-Object%252dOriented-ABAP-(2nd-Edition).html
    Although Java language has intuitive syntax, there are some special things in ABAP development so it is better to check solutions adjusted for ABAP editor.
    The most common usage of factory pattern is to simplify object creation.
    - By one method call you provide required parameters and do all initializations, including dependent objects.
    - Class can have many factory methods, if you want to provide more ways of initialization.
    - Factory method is usually static in the class and they return initialized instance of object for this class.
    - There is naming convention to start factory method name with "create" - easy to recognize pattern.
    - If you set property of class to "private instantiation" then you force to use factory method for object creation. In this way it is really simple to find all places where object are created with given set of input parameters - find references of factory method.
    Factory pattern becomes even more powerful if we add inheritance. Factory method returns basic object (like ZCL_VEHICLE) but its implementation can return different subclass instance, depending on input parameter (ZCL_CAR, ZCL_TRAIN etc). Each instance can implement differently behavior (methods implementation), but these are object oriented techniques.
    Regards,
    Adam

  • How to insert Method in ABAP code

    Hi Gurus,
    Plz tell me how to insert a METHOD in ABAP code as we do in the case of inserting function in ABAP code using PATTERN.Button
    Rgds
    rajesh

    Hi,
    Try this.
    Click pattern tab ans select AABAP object patterns.
    Select call method.
    Then give the instance name , class/interface name and the method name.
    Sharin.

  • Overriding Interface methods via JNI

    Hello,
    How we can override Interface methods via JNI?
    For example:
    At HelloWorldSwing example:
    http://download.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
    I've written anonymous Interface as:
    import javax.swing.*;       
    public class HelloWorldSwing1 {
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("HelloWorldSwing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Add the ubiquitous "Hello World" label.
            JLabel label = new JLabel("Hello World");
            frame.getContentPane().add(label);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        static Runnable runnableObject = new Runnable() {
             public void run() {
                  createAndShowGUI();              
       public static void main(String[] args) {
             //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(runnableObject);
    }via JNI functions(http://download.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html) we cannot override Runnable Interface's run method? Can we?
    Only:
    RunnableClass's value isn't null with given example at below. FindClass finds the Runnable Interface.
    jclass RunnableClass;
    RunnableClass = (*env)->FindClass(env,"java/lang/Runnable");
      if( RunnableClass == NULL ) {
        printf("can't find class Runnable\n");
        exit (-1);
      }But How can we override Runnable class's run method?
    Thanks in Advance

    The only way you can override a method in Java is by defining a class that does so, either statically or as an anonymous inner class. You can't do either of those things in JNI. Ergo you cannot do what you are asking about.

  • Accessing an app.module's client interface methods in other app module

    Hi all,
    We are developing an application with number of projects (one for each module). We have got a common project with an application module, entity and view objects which are common for the entire application. For reusability, this common project is shared in session scope since most of them are of static nature.
    In the shared application module's implementation class, a common method has been included that will be used for data filtering by constructing/appending where clauses in a particular application scoped view object. This method has been exposed as client interface method so that other projects can make use of it. The common project is compiled as an ADF Library jar for accessing it in other projects.
    For accessing the exposed client interface method, from the view controller layer, we can include its reference in the page definition file and can execute the method from a managed bean by finding the operation binding from the page definition. But, in case if we wish to access the method in another model project, we could not do so.
    Now, my questions are:
    1. Is it the correct location(Shared App Mod. Impl) for including the logic to filter the data?
    2. Is it possible to access the app. module impl method in another project's impl classes (Let it be entityImpl or viewObjectImpl or viewRowImpl or AppModImpl) ?
    3. If it is possible, how it has to be done?
    Thanks and regards,

    Strange, the same question here {thread:id=2187487}
    Timo
    Edited by: Timo Hahn on 08.03.2011 12:02
    @john You beat me agian

  • Static Classes/Methods vs Objects/Instance Classes/Methods?

    Hi,
    I am reading "Official ABAP Programming Guidelines" book. And I saw the rule:
    Rule 5.3: Do Not Use Static Classes
    Preferably use objects instead of static classes. If you don't want to have a multiple instantiation, you can use singletons.
    I needed to create a global class and some methods under that. And there is no any object-oriented design idea exists. Instead of creating a function group/modules, I have decided to create a global class (even is a abstract class) and some static methods.So I directly use these static methods by using zcl_class=>method().
    But the rule above says "Don't use static classes/methods, always use instance methods if even there is no object-oriented design".
    The book listed several reasons, one for example
    1-) Static classes are implicitly loaded first time they are used, and the corresponding static constructor -of available- is executed. They remain in the memory as long as the current internal session exists. Therefore, if you use static classes, you cannot actually control the time of initialization and have no option to release the memory.
    So if I use a static class/method in a subroutine, it will be loaded into memory and it will stay in the memory till I close the program.
    But if I use instance class/method, I can CREATE OBJECT lo_object TYPE REF TO zcl_class then use method lo_object->method(), then I can FREE  lo_object to delete from the memory. Is my understanding correct?
    Any idea? What do you prefer Static Class OR Object/Instance Class?
    Thanks in advance.
    Tuncay

    @Naimesh Patel
    So you recommend to use instance class/methods even though method logic is just self-executable. Right?
    <h3>Example:</h3>
    <h4>Instance option</h4>
    CLASS zcl_class DEFINITION.
      METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.
      METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.
    ENDCLASS
    CLASS zcl_class IMPLEMENTATION.
      METHOD add_1.
        e_output = i_input + 1.
      ENDMETHOD.
      METHOD subtract_1.
        e_output = i_input - 1.
      ENDMETHOD.
    ENDCLASS
    CREATE OBJECT lo_object.
    lo_object->add_1(
      exporting i_input = 1
      importing e_output = lv_output ).
    lo_object->subtract_1(
      exporting i_input = 2
      importing e_output = lv_output2 ).
    <h4>Static option</h4>
    CLASS zcl_class DEFINITION.
      CLASS-METHODS add_1 IMPORTING i_input type i EXPORTING e_output type i.
      CLASS-METHODS subtract_1 IMPORTING i_input type i EXPORTING e_output type i.
    ENDCLASS
    CLASS zcl_class IMPLEMENTATION.
      METHOD add_1.
        e_output = i_input + 1.
      ENDMETHOD.
      METHOD subtract_1.
        e_output = i_input - 1.
      ENDMETHOD.
    ENDCLASS
    CREATE OBJECT lo_object.
    lo_object->add_1(
    zcl_class=>add_1(
      exporting i_input = 1
      importing e_output = lv_output ).
    lo_object->subtract_1(
    zcl_class=>subtract_1(
      exporting i_input = 2
      importing e_output = lv_output2 ).
    So which option is best? Pros and Cons?

  • Static abstract methods - how can I avoid them?

    Hi everybody,
    just found out that java doesn't allow static abstract methods nor static methods in interfaces. Although I understand the reasons, I can't think of how to change my design to gain the required behavior without the need of static abstract methods.
    Here's what I want to do and how my thoughts lead to static abstract methods:
    ClassA provides access to native code (c-dll, using jna). The path to the dll can be set programmatically. Here's a draft of the class:
    public ClassA {
       private static String path;
       public static void setRealtivePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }There is some more classes which provide access to different dlls (therefore the code for accessing the dlls differs from ClassA). Although this works, the setRelativePath method has to be implemented in each class. I thought it would be nice to put this method into a abstract super class that looks like this:
    public abstract class superClass {
       public static void setRelativePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       //force inherting class to implement a static method
       public static abstract void setPath(String absolutePath);
    }thus simplifying ClassA and it's look-a-likes:
    public ClassA {
       private static String path;
       @Override
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }Since static abstract methods (and overriding static methods) is not allowed, this doesn't work.
    I hope someone can catch my idea ;). Any suggestions how to do this in a nice clean way?
    Thanks in advance,
    Martin
    Edited by: morty2 on Jul 22, 2009 2:57 AM

    First of all, thanks a lot for your answer.
    YoungWinston wrote:
    Actually, you can "override" static methods (in that you can write the same method for both a subclass and a superclass, providing the superclass method isn't final); it's just that it doesn't work polymorphically. The "overriding" method masks the overridden one and the determination of what gets called is entirely down to the declared type.Yes, I know that. There's one problem: Your suggestion means that I simply have to drop the abstract modifier in the super classes setPath method. However, since the super class calls the setPath method (and not the inherting classes!) it will always be the super classes' method being called.
    YoungWinston wrote:
    Why are you so concerned with making everything static? Seems to me that the simplest solution would be to make all the contents instance-based.I want ClassA and it's look-a-likes to be set up properly at application start up, be accessible quite anytime and easily, they don't do anything themselves except for setting the path and calling into the native library which does the math. (Compareable to how you call e.g. Math.cos()). Therefore I don't think that an instance-based solution would be a better approach.
    YoungWinston wrote:
    Furthermore, you could make the class immutable; which might be a good thing - I'm not sure I'd want someone being able to change the pathname after I've >set up a class like this.Thanks for that!
    PS: As mentioned in my first post, I do have a working solution. However, I'm really corious about finding a nicer and cleaner way to do it!
    Martin

  • Static interface Vs Static nested interface declaration

    Hi,
    I have observed that static interface declaration is not allowed in Java. However static nested interface declaration is allowed.
    I could not understood reason behind this behavior.
    e.g.
    interface A{
         void AA();
         public static abstract interface Aa{
    } ........... this is allowed in Java.
    static interface d{
    } ...... This is not allowed in Java...
    I already know that static mean one for class and interface has only abstract method.
    Please let me know if any one know resign behind this.
    Thanks .

    Why don't you go to a Java forum?

  • Static abstract method equivalent?

    From the forum thread:
    http://forum.java.sun.com/thread.jspa?forumID=31&threadID=5202376
    baftos wrote:
    On the othe hand OP had a legitimate desire.
    How can a superclass force its subclasses to
    provide a certain class (as opposed to instance) behaviour?
    Do other languages provide something like this?I like to have a static abstract method in a super abstract class:
    public static abstract Behavior getClassMarker();And each sub concrete class should have:
    public static Behavior getClassMarker(){
      return new Behavior(-- parameters --);
    }Could we have an equivalent that current Java allows? Or, do we see some good news on the horizon?

    Normally you create an instance with the metadata for the type, which annotations let you do:package dog;
    abstract class Dog {
      abstract String getBark() ;
      Dog () {
        assert (getBreed() != null);
      DogBreed getBreed () { return getClass().getAnnotation(DogBreed.class); }
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface DogBreed {
      String genericBark () ;
      String name () ;
    @DogBreed(name="Bulldog", genericBark="GrrrGrrrGrrr")
    class Bulldog extends Dog {
      String getBark() {
        return "Grrr";
    @DogBreed(name="Pomeranian", genericBark="Pirrr")
    class Pomeranian extends Dog {
      String getBark() {
        return "Sausages";
    public class DogAnnotationTest {
      public static void main (String...args) {
        Pomeranian pomme = new Pomeranian();
        System.out.println("The generic bark of the " + pomme.getBreed().name() + " breed is "
              + Pomeranian.class.getAnnotation(DogBreed.class).genericBark() + " but our  pomme cries "
              + pomme.getBark() + " for a jerky, his favorite food");
    }The limitation is that you can only annotate with simple types, so you're stuck with the abstract factory pattern if you want the creation. Not that that's a bad thing, as it lets you create instances of classes which haven't been loaded yet.

Maybe you are looking for

  • Converting a simple Oracle Report to XML Publisher report template

    Hello, I have a simple Oracle Report 9i which displays data from four tables. It uses three different queries to get the data. How can I simply generate a template file(say RTF) or atleast an XML source from that report? I tried joining the queries (

  • Magic Mouse need view all open applications....

    is it possible to have the same option to view the open applications in the new magic mouse, im a programer and always in need to haver several applications and run between them ... just bought the magic mouse its great loving it , but please insert

  • Stalled Scrolling and Other Problems

    Redo because not only does my thread seem to have disappeared, but also I wanted to start afresh. First off, are my threads even showing up anymore? Usually I get fairly prompt replies, but my last couple of threads have gotten nothing. While I am so

  • Where has all the 'help' gone?

    I just commented on an earlier thread about the sudden disapearance of  online "help" from within Photoshop CS4. Why? Now when I click on Help I get routed to a generic Adobe page where I have to burrow down through newer versions to find CS4, then h

  • Reinstall Adobe Acrobat Pro

    I replaced my laptop with one without a disc drive.  How do I reinstll my Adobe purchase?