Application Development 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: 

Why can't we redefine an inherited static method?

ChrisPaine
Active Contributor
0 Kudos

Hello,

I recently (yes I'm slow) came across the need to have two pieces of very similar functionality for doing employee searches. One would allow all employee's within the user's authority to be returned, another would filter this to a certain subsection of the employee population.

Now SAP define a standard interface for this employee search (when using OADP) which is IF_HRWPC_OADP_OBJECTSEARCH.

This particular interface contains 6 static methods. Now I, being the numpty that I am, thought - this is fine, I'll define my generic search class which implements this interface first, then I'll subclass it and redefine those bits I need to to filter the results.

On creation of subclass - try to redefine the methods - error message -> cannot redefine static methods.

So my filter class will instead of calling a superclass, just create an instance of the generic class and call its methods.

So now I have to ask...

Why?

What possible benefit is there to the programming model in not allowing redefinition of a static method?

I had an interesting read of in which i see that this issue is potentially down to the way in which SAP has implemented inheritance for statics (as in they haven't - the static component still remains part of the parent not the child). But why do this? (not allow static methods to be redefined), surely it wouldn't be too difficult to fix and it would add value to the language?

I look forward to your responses - I'm sure I will learn something.

Thanks,

Chris

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Chris

Unfortunately, I don't have an answer for you but I do share your pain.

I have recently tried to create some (in my humble opinion :-P) sophisticated Business Classes that use the Singleton pattern for instantiation. I've not found a way, or at least not a simple way to create an inheritance hierarchy of Businesses Classes and allow the static GET_INSTANCE methods to be called "polymorphically" - that is, if I have a reference to a sub-class then the sub-class' GET_INSTANCE method but that sub-class GET_INSTANCE can call the super-class' GET_INSTANCE to instantiate the common attributes, etc. Maybe I should be using a Class Interface in some way but I'm not sure that solves the whole problem... anyway I haven't worked it out yet...

I'll be watching this thread to see what answers may surface!

Regards

Glen

11 REPLIES 11

Former Member
0 Kudos

Hi Chris

Unfortunately, I don't have an answer for you but I do share your pain.

I have recently tried to create some (in my humble opinion :-P) sophisticated Business Classes that use the Singleton pattern for instantiation. I've not found a way, or at least not a simple way to create an inheritance hierarchy of Businesses Classes and allow the static GET_INSTANCE methods to be called "polymorphically" - that is, if I have a reference to a sub-class then the sub-class' GET_INSTANCE method but that sub-class GET_INSTANCE can call the super-class' GET_INSTANCE to instantiate the common attributes, etc. Maybe I should be using a Class Interface in some way but I'm not sure that solves the whole problem... anyway I haven't worked it out yet...

I'll be watching this thread to see what answers may surface!

Regards

Glen

MarcinPciak
Active Contributor
0 Kudos

Hi Chris,

For me:

STATIC means something defined for all the instances which is changed for all of them or for none.

In contrary

INSTANCE means something specific for certain entity, defined exclusively for its use.

Let's think of an example: a car plant (forgive my simplicity). A famous car brand (no I won't be addvertising any;) ) has launched a specific car model within 2005-2010 years in 3 series. In years 2005-2007 first one, 2008-2009 second, 2010 - onwards a third one. In each serries the engineers did small corrections (in response to drivers complaints about some technical disabilities). They also decided to add some new facilities according to new business management strategy. Some things, however, the engineers spoted to be perfectly designed from the very beggining. They decided to keep them unchanged in all the series. At the end of the day all 3 series had some components installed exactly the same (and from the same producer), while the rest has been enhanced a bit or replaced with new better ones.

I have we have a similar situation here. All the part which have stayed in all 3 serries are our static components and methods. All the rest are series specific. So the parent class would be an abstract plan for producing all the cars in that line. It means that those newer which have been enhanced (made a child of the former parent plan) inherit all the static parts along the series. Once it had been changed in any series the engineers couldn't say "we used the same parts in all of them" any more. Any change would make them series specific (instance). That's why (in my opinion) static must stay static along all hierarchy tree.

I think this link gives nice feeling what I am trying to express. http://demotywatory.pl/738390/BMW-dopracowywane-latami

Translation:

BMW - enhanced through the years, Lada - perfect from the very beggining

Regards

Marcin

A small correction for STATIC definition in order not to confuse it with CONSTANT.

Edited by: Marcin Pciak on Oct 15, 2010 1:32 PM

0 Kudos

Hello Chris,

When i read the concepts of inheritance in OO-ABAP, frankly this question never occured to me.

For me STATIC methods are like Function Modules, the external user can use them knowing that their behaviour will remain AS-IS wherever he uses them.

If REDEFINITION is allowed, then this will be a violation of the underlying concept(as mentioned above) of STATIC methods.

Hope this helps.

BR,

Suhas

matt
Active Contributor
0 Kudos

There is no definitive reason, except "it's been designed like that". Java allows static methods to be redefined, but not overridden (i.e. you can't change the signature).

matt

0 Kudos

Hello Matt,

This means in Java the static method(if it has been re-defined) of the Super Class & the Sub Class will behave differently.

Is this desirable? Your comments!

BR,

Suhas

matt
Active Contributor
0 Kudos

The question is a bit like "In C++ you can have multiple superclasses - is this desirable". Obviously, to the designers of C++, it was. For the designers of Java, where multiple inheritance like that isn't possible, it wasn't desirable.

Allowing redefinition of static methods can lead to problems. But it can also be useful. For the ABAP OO developers, however, it wasn't desirable.

0 Kudos

All - thank you so much this is all very informative and interesting!

now some specific thoughts:

STATIC means something defined for all the instances which is changed for all of them or for none.

to me static = singleton - only a single instance of object allowed in a memory space. This seems to be the case in Java, but not ABAP. If I wanted to only have a single representation (no polymorphism allowed) of a certain logic I would define it as FINAL. To me there seems to be a confusion in ABAP about the roles of these two flag.

Indeed - the

Rule 5.3: Do Not Use Static Classes

seems to exist because of the limitations of the ABAP implementation - and does not give us an advantage. We can see many pieces of SAP code that follow this logic of having a static factory class to create a singleton instance of processing logic - therefore allowing for different implementations of the same interface. It seems to me that were it allowed to re-implement static methods there would be no need to have these strange artificially singleton classes. The only way that they enforce their singleton nature is through complex constructors that can only be created by the static/singleton factory classes. There is nothing explicit in the class that shouts out - "I'm singleton!" (or perhaps there is and I'm a bit blind).

It is complex code pattern to follow if you do want polymorphic singleton behaviour from your classes - would it not be simpler - as in Java (you may notice I have a slight leaning here - you should see the number of times I've requested a true Boolean in ABAP and been so surprised when ABAP developers just don't see the need) - to continue for my aside - as in Java, just define something as static if you want it to be singleton and final if you don't want it to be redefined.

I can understand - referring to the example given - that there are pros and cons of allowing multiple superclasses - although personally I like the idea - a class for flying animals and a class for pigs, both subclassed in a flying pig class - I can have have multiple interfaces, why not multiple superclasses? I digress For that case, I can see that there are arguments both ways - but for the case of static methods not being able to be redefined I can't see the value.

Perhaps I'm being to simplistic and idealistic - and perhaps I just don't fully get the semantics of what static, final and instance mean in the ABAP context. Perhaps my question should be instead - why can't we easily define a polymorphic singleton method in ABAP...

Thanks for all your answers so far - very entertaining as well as informative.

Cheers,

Chris

matt
Active Contributor
0 Kudos

>static = singleton

The statement doesn't really make sense. A singleton is a design pattern. A static is an characteristic of a component of a class. When I'm coding in Java, I use a static attribute to hold the reference, and a static method to allow it to be requested, but all other methods relating to the object are instance methods. Also, while I guess it's possible to implement a singleton using statics, statics have a wider use.

I came to OO programming via Java, so I have a bias towards it as well. I was saying just the other day to a colleague how nice it would be if ABAP had a proper boolean type, enabling us to write

IF me->meets_some_condition( ).

rather than

IF me->meets_some_condition( ) EQ abap_true

.

>why can't we easily define a polymorphic singleton method in ABAP...

Good question. And that really cuts to the heart of it.That is, it seems to me, a good reason to allow inheritance of statics.

0 Kudos

Matt,

Thanks for your comments - I mentioned in my original post that I'd learn something from this discussion, and I have.

Unfortunately, it doesn't seem that we've actually come up with an answer - just reformatted my original uneducated question...

If you or anyone else reading is at SAP TechEd at the moment perhaps this might be a pointed question to ask - along with the boolean please

Thanks again for the responses - I think I am learning about the differences between singleton and static.

Cheers,

Chris

Edit - I'm going to close this post as "unanswered" - please do not take this as diminishing any of the responses given - it's just that unless I take the "Because SAP hasn't defined it that way" as a valid answer - I don't think it will ever be possible to answer (unless SAP do a bit of redesign! - here's hoping!)

Thanks again for the multiple very interesting and well stated answers! You guys rock!

Cheers,

Chris

Edited by: Chris Paine on Nov 12, 2010 12:20 PM

Former Member
0 Kudos

Chris,

I happened to read a recent SAP publication authored by Horst Keller. I am quoting some of what is mentioned in the book.

There are lot of rules listed in the book. One such rule is:

Rule 5.3: Do Not Use Static Classes

Preferably use objects instead of static classes. If you don't want to have multiple instantiation, you can use singletons.

It lists couple of reasons to prefer object creation instead of static classes;

a. They remain in memory, as the time of initialization cannot be controlled

b. Static constructors have limited functionality since they have no parameter interface to propagate any exceptions

It further says,

static classes spoil your polymorphism options. On the one hand, you cannot redefine static methods; on the other hand, there is no access via reference variables, the other "pillar" of polymorphism. Even if you initially don't plan to overwrite the behavior of a method, this request frequently arises during further development.

And suggests,

To keep the option of redefinition open, always use instance methods instead of static methods

naimesh_patel
Active Contributor
0 Kudos

I found a reference in the ABAP Keyword Help in this page: http://help.sap.com/abapdocu_70/en/ABAPCLASS-METHODS_GENERAL.htm but it doesn't give us any reasoning.

Note

You cannot redefine static methods. This means that you cannot define them as abstract or final.

Regards,

Naimesh Patel