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
Similar Messages
-
It seems that I cannot redefine a static method in a subclass in ABAP OO, right?
Is there a reason for that? Is this like this in other languages as well?That's true. You cannot redefine static methods in ABAP OO.
I can add that a class that defines a public or protected static attribute shares this
attribute with all its subclasses.
Overriding static methods is possible for example in Java.
This simple piece of code illustrates this:
public class Super {
public static String getNum(){
return "I'm super";
public static void main(String[] args) {
System.out.println("Super: " + Super.getNum());
System.out.println("Sub: " + Sub.getNum());
public class Sub extends Super{
public static String getNum(){
return "I'm not";
The output is:
Super: I'm super
Sub: I'm not
When overriding methods in Java you must remember that an instance method cannot override a static method, and a static method cannot hide an instance method.
In C# a static member can't be marked as 'override', 'virtual' or 'abstract'. But it it is possible to hide a base class static method in a sub-class by using the keyword 'new':
public class Super
public static void myMethod()
public class Sub: Super
public new static void myMethod() -
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 -
From Classic abap FMs to ObjectOriented instance/static methods: reference
Hi All,
we are migrating one of our project developed using classic abap to abap objects.
mostly we are replacing the Function modules with either instance methods or static methods.
for e.g.
if there are FMs like GUI_UPLOAD, SO_NEW_DOCUMENT_SEND_API1 are used, after exploring in sdn for abap objects, we realized
we can use cl_gui_frontend_services=>gui_upload( ) instead of the GUI_UPLOAD.
send_request = cl_bcs=>create_persistent( ). send_request->send( ). can be used instead of SO_NEW_DOCUMENT_SEND_API1
like that there are several FMs present, is there any easy way where we can find this reference of object oriented methods corresponding to the plain old Function modules of classic abap.
i was thinking, after seeing the blogs of people like Thomas Jung, Horst Keller, they encouraged a lot on abap objects over procedural abap. are there any such guidlines, references present to migrate (rather upgrade) to abap objects from existing procedural abap
thanks,
Madhu_1980I don't think it makes sense to do 1:1 mapping and replacement in your code.
It does make sense to refactor using Object oriented techniques and patterns but it is usually a non-trivial task. -
OOPs Concept ! - Why Static Methods can't be redefined in its subclass ?
Dear Experts ,
I had searched the SDN , but unable to find out the satisfactorily answers ..
Can anybody let me know the reason for the following Confusion in Oops Concept
Question 1: As we know , We can Inherit the Static Methods in the Sub Class , But we can't redefine it in the base class or Sub Class ?
Question 2 : Why can't Static Method be Abstract ?
Question 3 : Can a Class be Abstract or Final Both, If yes, then why ?
Thanks in Advance
Saurabh GoelAs per the above discussion two of your doubts have already been clarified so I am taking only third one.
A class cannot never be Abstract and final both coz Abstract signifies that the implementation of the class has not been defined completelyi.e. may be some methods have been defined but few methods are still missing implementation. 'Final' is used for those classes/methods which cannot be redefined means the complete implementation of the method has been defined no one can implement further logic under the same method.
If you are saying your method is Final then it cannot be overridden and Abstract implies that method implementation is yet to be defined which can only be implemented if that class/method is not 'Final'. So both the terms are contradictory.
Hope it clarifies!!!
Thanks,
Vishesh -
Hello,
i discovered that method calling in the class-inheritance of ABAP Objects doesn't work similar to other OO languages.
Example:
1 Superclass named "Z_CLASS"
1 Subclass of "Z_CLASS" named "Z_SUB"
Z_CLASS defines a method "initialize" and has a constructor which calls "initialize".
Z_SUB redefines the method "initialize" assigning some values to instance-attributes.
If i instantiate a new "Z_SUB"-object then the contructor of "Z_CLASS" is called which calls ITS OWN "initialize" not the one of the subclass. For comparison: Java would call the initialize of the subclass.
I have to implement a constructor in every subclass of "Z_CLASS" which calls the super constructor and the own "initialize" after that to solve this problem. This isn't the proper OO way!
Did i miss something or am i right?
Thanks in advance,
StefanCheck out this, it might be helpfull...
INTERFACE I_COUNTER
INTERFACE i_counter.
METHODS: initialize.
ENDINTERFACE.
CLASS C_COUNTER1 DEFINITION
CLASS c_counter1 DEFINITION.
PUBLIC SECTION.
INTERFACES i_counter.
METHODS: constructor.
DATA count TYPE i.
ENDCLASS.
CLASS C_COUNTER1 IMPLEMENTATION
CLASS c_counter1 IMPLEMENTATION.
METHOD constructor.
CALL METHOD me->i_counter~initialize.
ENDMETHOD.
METHOD i_counter~initialize.
count = count + 10.
write:/ 'count from super', count.
ENDMETHOD.
ENDCLASS.
CLASS C_COUNTER2 DEFINITION
CLASS c_counter2 DEFINITION INHERITING FROM c_counter1.
PUBLIC SECTION.
METHODS: constructor,
initialize.
DATA count1 TYPE i.
ENDCLASS.
CLASS C_COUNTER2 IMPLEMENTATION
CLASS c_counter2 IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
CALL METHOD me->initialize.
ENDMETHOD.
METHOD initialize.
count = ( count / 10 ).
write:/ 'count from sub', count.
ENDMETHOD.
ENDCLASS.
DATA: count TYPE REF TO c_counter2.
START-OF-SELECTION.
CREATE OBJECT count TYPE c_counter2. -
How to call a static method from an event handler
Hi,
I'm trying to call a static method of class I designed. But I don't know how to do it. This method will be called from an event handler of a web dynpro for Abap application.
Can somebody help me?
Thx in advance.
Hamza.To clearly specify the problem.
I have a big part code that I use many times in my applications. So I decided to put it in a static method to reuse the code. but my method calls functions module of HR module. but just after the declaration ( at the first line of the call function) it thows an exception. So I can't call my method. -
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
SudhansuHi 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 -
Remote-Enabled Method (wrt ABAP Objects)
Hi All,
Adhering to ABAP Objects, in my project, i am creating instance methods or static methods instead of Function modules.
now i have a requirement, where some functionality needs to be invoked on another server using an RFC destination.
if i create a function module,
1)i can tick the radio button "Remote-Enabled Module" under the Attributes of function module and
2)i can invoke it as
CALL FUNCTION 'ZMY_FM' DESTINATION 'RFCDESTNAME'
EXPORTING
IMPORTING
in such cases is it mandatorily required to create an FM like above, or can i avoid it by writing an equivalent instance method or static method
if we replace the FM with a method, then
1)how can i make the method rfc enabled, and
2)what is the syntax of providing the rfc destination name while invoking method.
Is there any other concept provided in ABAP Objects, that is recommended instead of "Remote-Enabled Function Modules"
thanks in advance,
Madhu_1980Hello Madhu
If you look at the properties of any global class you will find that the DropDown list for "Category" contains item:
30 = Proxy Class for Remote Interface
For me this sounds like that there might be something in the pipeline of SAP to provide RFC-enabled methods. However, if and when this becomes true is probably unknown.
Regards
Uwe -
Static methods vs instant methods
hi to all abap gurus
thanks in advance
all of the objects in the class can acess its its static attributes . and if u change the static attribute in an object the change is visible in all other objects in the calsssss.
can u pls expain this and tell the diffrence bewteen static and instance metod s?Hi,
<b>Instance Method</b>
You can declare instance methods by using the METHODS statement. They play a very important role as they can access all of the attributes of a class and can trigger all of the events of the class.
<b>Static Methods</b>
You can declare static methods by using the CLASS-METHODS statement. They are important and can only access static attributes and trigger static events.
<b>STATIC METHODS</b>
CAN ONLY USE STATIC COMPONENTS IN THEIR IMPLEMENTATION PART
CAN BE CALLED USING THE CLASS
Static methods (also referred to as class methods) are called using CALL METHOD <classname>=><class_method>.
If you are calling a static method from within the class, you can omit the class name.
You access static attributes using <classname>=><class_attribute>
Regards,
Padmam. -
Static Methods, the case of HashMap
static int hash(Object x) {
int h = x.hashCode();
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;I was cruising the HashMap class. And I noticed right away that all non interface methods were static package methods. Now isn't that bad OOP practice? Right. It breaks the inheritance relationship.
It means I cannot make a subclass of HashMap and re-write that hash function. Was that intentional?
on a OOP perspective or
on a down to earth matter,
Are static method more performant in speed? Or memory ( i guess yes here)? or what?I was cruising the HashMap class. And I noticed right
away that all non interface methods were static
package methods. Now isn't that bad OOP practice?
Right. It breaks the inheritance relationship.
It means I cannot make a subclass of HashMap and
re-write that hash function. Was that intentional?If they are package-private, I take it for granted they didn't want application developers to redefine these methods.
If they are static, they are also preventing themselves (Javasoft) to override them in subclasses that would be included in the JDK.
I'd say in both cases, that it is intentional, since, at least in the case of the hash function, the intent is to disperse hash results even for low-dispersing custom hash functions, as was explained in another thread in ALT (wasn't it you already? ;-).
This is maybe questionable, but defendable, that they didn't want any subclass to mess with these "optimizations".
You can regard this as poor OO practice, but this is no more hampering you that if they had declared the method as private.
I acknowledge that declaring the method private has the advantage of making explicit that they don't want anyone to override it.
But by declaring it static package, they ensure no-one can override it, so the other methods in HashMap relying on this method are guaranteed that no subclass can mess with it, but other classes in the same package (say, WeakHashMap, HashSet,...), can call it. all the same
Are static method more performant in speed? Or memory
( i guess yes here)? or what?Static methods are not faster in themselves, but invoking them may be, since they can be "statically" resolved.
When the calling class is loaded in memory, the callee is loaded too, and the interpreter or JIT or HotSpot is free to inline the static method's body (no indirection).
Even if it doesn't, invoking the static method implies to moving the program counter or pointer, or whatever, to an adress that has already been resolved once and for all (one indirection),
whereas invoking a virtual method implies a double indirection, looking up the address in some kind of dynamic table (one per class, determined by the target object's leaf class).
There are indeed two different bytecodes for these two situations! -
Regarding Returning Parameter and Static method
Hi frnds,
I am learning oops ABAP. I want to use returning parameters in method and also STATIC METHOD.
If any example it will be more helpful
regards,
satyaHi satya,
Check this out ,Its helpful.
To get some values from a method , one can use the exporting, changing or returning parameters.If one uses RETURNING parameters, the following restrictions apply:-(1) No EXPORTING/CHANGING parameters can be used for the method.(2) Only one RETURNING parameter can be used.(3) RETURNING parameters are only passed by value.This program demonstrates the use of RETURNING parameters and the various ways to call a method with RETURNING parameter to get the value into some variable.
Sample Program
</code>
report ysubdel1 message-id 00.
data : w_num type i.
class c1 definition .
public section.
methods : m1 importing input1 type i
input2 type i
returning value(result) type i .
endclass.
class c1 implementation.
method : m1.
result = input1 * 2 + input2.
endmethod.
endclass.
start-of-selection.
data : obj1 type ref to c1 .
create object obj1.
Syntax 1
call method obj1->m1 EXPORTING input1 = 5 input2 = 4
RECEIVING result = w_num.
write:/5 w_num .
Syntax 2
w_num = obj1->m1( input1 = 10 input2 = 20 ).
write:/5 w_num .
Syntax 3
move obj1->m1( input1 = 2 input2 = 3 ) to w_num .
write:/5 w_num .
</code>
Static method example
<code>
REPORT zstatic. .
data : num type i.
class testclass definition.
public section.
class-methods : testmethod.
endclass.
class testclass implementation.
method : testmethod.
num = 5.
write:/5 num.
endmethod.
endclass.
start-of-selection.
call method testclass=>testmethod.
</code>
Reward Points if u find helpful.
Thnks & Regards,
Rajesh -
Is it possible to redefine a method of a class within an already instantiated instance of that class? For example if I have ...
class Dog{
public boolean isBigDog(int width, int height){
return true; // always says its a big dog
public void sitOnCouch(){
if(isBigDog(with,height){
System.out.println("Dog can sit on the couch");
}else{
System.out.println("Dog CAN'T sit on the couch");
public static void main(String[] args){
Dog dog = new Dog();
dog.sitOnCouch(); // prints out "Dog can sit on the couch"
//now if I didn't want that same dog, not a new one, to sit on the couch how can I redefine isBigDog()
//for that instance of dog?
}What I am really trying to do is to redfine the isCellEditable(int row, int col) in the DefaultTableModel class on an already instantiated DefaultTableModel. User click's a check box to allow cells to be editable or not to be editable.AwhHawh.... GOT IT! In case anyone cares for this
bit of code it will "change cell editable on the fly".Not bad at all. However it has the side effect of creating a whole new model and throwing away
the old one. Let me suggest this instead, as per my previous post recommending a subclass:public class MyDefaultTableModel extends DefaultTableModel {
// local variable to store "editable" status
private boolean isEditable = true;
public MyDefaultTableModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
public DefaultTableModel(Object[] columnNames, int rowCount) {
super(columnNames, rowCount);
// implement any other constructors you need like the above
// method to change "editable" status
public void setAllowEditing(boolean allow) {
isEditable = allow;
// override isCellEditable to use our private variable
public boolean isCellEditable(int row, int column) {
return isEditable;
}Hope this helps.
PC² -
Compilation error while calling static method from another class
Hi,
I am new to Java Programming. I have written two class files Dummy1 and Dummy2.java in the same package Test.
In Dummy1.java I have declared a static final variable and a static method as you can see it below.
package Test;
import java.io.*;
public class Dummy1
public static final int var1= 10;
public static int varDisp(int var2)
return(var1+var2);
This is program is compiling fine.
I have called the static method varDisp from the class Dummy2 and it is as follows
package Test;
import java.io.*;
public class Dummy2
public int var3=15;
public int test=0;
test+=Dummy1.varDisp(var3);
and when i compile Dummy2.java, there is a compilation error <identifier > expected.
Please help me in this program.public class Dummy2
public int var3=15;
public int test=0;
test+=Dummy1.varDisp(var3);
}test+=Dummy1.varDisplay(var3);
must be in a method, it cannot just be out somewhere in the class! -
Using a non-static vector in a generic class with static methods
I have a little problem with a class (the code is shown underneath). The problem is the Assign method. This method should return a clone (an exact copy) of the set given as an argument. When making a new instance of a GenericSet (with the Initialize method) within the Assign method, the variables of the original set and the clone have both a reference to the same vector, while there exists two instances of GenericSet. My question is how to refer the clone GenericSet's argument to a new vector instead of the existing vector of the original GenericSet. I hope you can help me. Thanks
package genericset;
import java.util.*;
public class GenericSet<E>{
private Vector v;
public GenericSet(Vector vec) {
v = vec;
private <T extends Comparable> Item<T> get(int index) {
return (Item<T>) v.get(index);
public static <T extends Comparable> GenericSet<T> initialize() {
return new GenericSet<T>(new Vector());
public Vector getVector() {
return v;
public static <T extends Comparable> GenericSet<T> insert (GenericSet<T> z, Item<T> i){
GenericSet<T> g = assign(z);
Vector v = g.getVector();
if (!member(g,i))
v.addElement(i);
return g;
public static <T extends Comparable> GenericSet<T> delete(GenericSet<T> z, Item<T> i){
GenericSet<T> g = assign(z);
Vector v = g.getVector();
if (member(g,i))
v.remove(i);
return g;
public static <T extends Comparable> boolean member(GenericSet<T> z, Item<T> i) {
Vector v = z.getVector();
return v.contains(i);
public static <T extends Comparable> boolean equal(GenericSet<T> z1, GenericSet<T> z2) {
Vector v1 = z1.getVector();
Vector v2 = z2.getVector();
if((v1 == null) && (v2 != null))
return false;
return v1.equals(v2);
public static <T extends Comparable> boolean empty(GenericSet<T> z) {
return (cardinality(z) == 0);
public static <T extends Comparable> GenericSet<T> union(GenericSet<T> z1, GenericSet<T> z2) {
GenericSet<T> g = assign(z1);
for(int i=0; i<cardinality(z2); i++) {
Item<T> elem = z2.get(i);
insert(g, elem);
return g;
public static <T extends Comparable> GenericSet<T> intersection(GenericSet<T> z1, GenericSet<T> z2) {
GenericSet<T> g = initialize();
for(int i=0; i<cardinality(z2); i++) {
Item<T> elem = z2.get(i);
if(member(z1, elem))
insert(g, elem);
return g;
public static <T extends Comparable> GenericSet<T> difference(GenericSet<T> z1, GenericSet<T> z2) {
GenericSet<T> g = initialize();
for(int i=0; i<cardinality(z1); i++) {
Item<T> elem = z1.get(i);
if(!member(z2, elem))
insert(g, elem);
for(int i=0; i<cardinality(z2); i++) {
Item<T> elem = z2.get(i);
if(!member(z1, elem))
insert(g, elem);
return g;
public static <T extends Comparable> GenericSet<T> assign(GenericSet<T> z) {
GenericSet<T> g = initialize();
for(int i=0; i<cardinality(z); i++) {
Item<T> elem = z.get(i);
insert(g, elem);
return g;
public static <T extends Comparable> boolean subset(GenericSet<T> z1, GenericSet<T> z2) {
for(int i=0; i<cardinality(z1); i++) {
Item<T> elem = z1.get(i);
if(!member(z2, elem))
return false;
return true;
public static <T extends Comparable> int cardinality(GenericSet<T> z){
Vector v = z.getVector();
return v.size();
}The issue is not "reference a non-static interface", but simply that you cannot reference a non-static field in a static method - what value of the field ed would the static method use? Seems to me your findEditorData should look something like this: public static EditorBean findEditorData( String username, EditorBean editorData )
return editorData.ed.findEditor( username );
}
Maybe you are looking for
-
How do I get my 'Compilations' storage folder to match the view by artist list in the library?
When I view by Artist in the iTunes library, I have a 'compilations' section that contains soundtracks, etc. The weird thing is that when buying and or importing cds of this nature, it categorizes on my hard drive differently. I have 30 albums in my
-
Hp Laserjet CP3525 Firmware update failure
I was trying to update the firmware on an HP color Laserjet CP3525 using a USB connection using the instructions that were posted on the HP website for the Firmware upgrade. It looked like everything was going well, but when the printer rebooted it
-
Application Server Download, Upgrdation 4.6b to ECC 6.0
Hi Experts, We are undergoing upgrade from 4.6b to ECC6.0, OPEN DATASET APPL_FILE FOR OUTPUT IN TEXT MODE. This statement is written in 4.6b version, the ouput is perfect. In ECC 6.0 the statement is OPEN DATASET APPL_FILE FOR OUTPUT IN TEXT MODE ENC
-
I just bought my first MacBook Pro a few days ago. I can't seem to get my PC to see my MacBook Pro on the network. Can anyone help me get my PC to see my MacBook Pro on my network t? Thanks, Alex
-
We are getting the below error in our production server & also other servers(UAT, Staging). Only option is to restart the OSB server. Test connection is working fine and able to connect the database without any issue. We have enabled connection leak