tags:

views:

191

answers:

4

why we should not use static and abstract for a single method?

+6  A: 

the static keyword is defined so that a method can be called by a class name rather then an object. that means the method has to have some sort of definition. but abstract means you do not have any details about what the method does, it is as it says **Abstract**. When you inherit or extend a class you can then define the method.

Think of an interface.

If you are asking about having a static method inside of an abstract class, that is a different story. An abstract class is essentially as mentioned an interface and contains just a template of say functions that you must later on implement by inheriting / extending the class. Once you extend that class the static method does not come along with it (that is by default unless the access modifier is public / protected).

JonH
"the static keyword is defined so that a method can be called by a class name rather then an object." This somewhat misses the full meaning. A static method belongs to the class itself, not to instances *of* the class.
matt b
@JonH - was that patronizing answer really needed? While your definition was not wrong, matt b's was more precise than yours. I hardly think that means he needs to read up on object oriented programming.
Joel Mueller
@Joel Mueller - There was nothing wrong with what I said, sorry you took offense. I said if he wants to understand it some more to read into OOP.
JonH
@JonH - And I'm saying that nothing he said gives any indication that he needs to understand it some more. It makes you look bad when you patronize someone who clearly understands the topic at least as well as you do, if not better. So save your weaselly "sorry you took offense" non-apology - I'm not the one you insulted.
Joel Mueller
@Joel Mueller - Not sure what your vendetta is with me, but whatever floats your boat. If I put your brain in a parrot's head it would probably fly backwards.
JonH
@JonH - thanks for the petty downvote on an unrelated issue, and the bonus insult. Once again you demonstrate your class and charm. I don't have a vendetta against you, I was just pointing out that you were being rude. You, of course, responded with more rudeness. I shouldn't be surprised, but I guess I read too much into your avatar icon.
Joel Mueller
@Joel Mueller - I didn't down vote you.
JonH
Let me rephrase that I did downvote you here: http://stackoverflow.com/questions/696919/how-do-you-deal-with-new-features-of-c-so-that-they-dont-lead-to-poorly-written/698259#698259 and for clear reasons, as you were off topic. I guess others noticed that too. I think you've had a bad day today so I am not sure why you randomly came on here and attacked me. But I hope tomorrow is better.
JonH
@Joel Mueller - What's wrong with my avatar?
JonH
@JonH - Nothing at all is wrong with your avatar. It just led me to make some incorrect assumptions about the quality of your character. I thought you were being unintentionally rude to matt, and would appreciate it if someone pointed it out, giving you an opportunity to correct your mistake. Clearly I was wrong, as now I'm the one who's the target of patronizing remarks. I'll just leave you to it, then.
Joel Mueller
@Joel Mueller - I guess you assume a lot of things in that I was not being rude to matt. I merely pointed out becoming more familiar with OOP. I to this day read a lot about OOP so I see no issue with others telling me to read about it. I just found it strange for a thread this old for someone to come in and start arguing? I mentioned to you earlier that I was sorry if you took offense to it, apparently matt b didn't and he shouldn't because I was simply giving him information. Then to start mentioning avatars is kind of childish. So please stop trolling.
JonH
One last try: Matt offered a clarification to your point. You rejected his clarification and told him to read up on the subject, thereby implying that Matt's clarification was wrong, and he didn't know as much as he should about the topic. That was both incorrect, and insulting. In addition, "I'm sorry you took offense to it" is not an apology, it's a politician's trick. Call me childish if you want, but I'm not the one doing the name calling here. This thread can stand for itself - I've got nothing more to add to it.
Joel Mueller
@Joel Mueller - Are you hoping for me to say you win, because to me its about helping users and other developers..definately not a contest. If you want the "Hey Joel You're RIGHT!" you can have it. But please stop trolling the forum. You misread something and made an assumption and called me out on it. I stated that I was sorry you were offended. Apparently something is wrong with you as that wasn't enough. Give up the childish insults and do the site a favor, be on here to help and learn rather then pick fights.
JonH
*shrug* - if someone pointed out to me that something I said could be interpreted as insulting, and I hadn't meant to be insulting, I would just say, "Sorry, I didn't mean to be insulting. Allow me to rephrase." - which is very different from "I'm sorry you were offended." And that would be the end of it. You chose to drag this out, and I wasn't the one making childish insults. I mean really - parrots? But for my part, I am sincerely sorry that I got into this. It has clearly done nobody any good. In future, I will leave you strictly alone.
Joel Mueller
@Joel Mueller - Thanks and likewise
JonH
+3  A: 

A static method is not inherited. Therefore, making it abstract is a nonsense.

KLE
static methods are inherited. A static method in abstract class Foo can be called as class method on both Foo and Bar that extends Foo.
Carl
They are only accessible, without prefixing with the name of the superclass. But they are not inherited. Example: you define a method with the same name in the subclass ; an instance of the subclass, while executing a method in the superclass, calls the method without prefix : the superclass method is called! If it was inherited, the redefined method would be used. This proves that the two methods are not related, except by syntaxe.
KLE
+3  A: 

The abstract keyword means that child classes must override the method - this is (one of the ways) Java supports polymorphism. If you want to make it so that subclasses cannot override the method you mark it final. So it would be impossible to have an "abstract final" method since they are the exact opposite of each other.

the static keyword implies final as well - all static method are also final. Thus it is impossible to have a method that is both static and abstract since you would be able to make a method that is abstract and final.

The reason for static being final is that it is bound to the class instead of the instance. That means that the compiler looks it up at compile time rather than runtime to determine which method to call. The reason what it is like that? Arbitrary decision that the designers of Java made - they could have allowed static method to be overridden but decided not to. I don't have any particular insight as to why the chose one over the other unfortunately.

TofuBeer
A: 

As others have said, static+abstract is nonsense in Java. But there have been (rare) occasions where I've wished I could do just that.

The result I was looking for was basically to say that... "all concrete classes that extent this abstract class (or implement this interface) must provide a static method with this signature." This capability would allow these classes to provide meta-information about themselves.

Normally I have ended up with an instance method in these cases. If you stipulate that concrete implementations must support the default (no-arg) constructior, you can do...

MyInterface obj = MyClassThatImplementsMyInterface.newInstance();
obj.invokeTheMethodIWishWasBothStaticAndAbstract();
Drew Wills