5

..and if so what is the behavior? I came across this in some code I was looking at recently, and it is very confusing to me. I don't have a java compiler, so I can't answer this easily myself. Here is the rough example of what I'm talking about. I would expect this result in a compile error, but as far as I know it is from a working code base.

abstract class Base {
    ...
    abstract boolean foo(String arg);

}

class Sub extends Base {
    ...
    boolean foo(String arg) {
        if(condition) 
            return true;
        else 
            return super.foo(arg); //<-- <boggle/>
    }
}
MarkPflug
  • 25,238
  • 6
  • 40
  • 48

5 Answers5

17

No, if it's abstract in the superclass you can't call it. Trying to compile your code (having fixed the others) gives this error:

Test.java:13: abstract method foo(String) in Base cannot be accessed directly
            return super.foo(arg); //<-- <boggle/>
                        ^
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • 1
    That is what I thought, thanks. Like I said, I don't have a compiler, and typed the code directly into the code pane (I'm not a Java developer). – MarkPflug Feb 15 '11 at 18:13
  • consider the struscute abstract class A - > class B extends A and implements Foo -> C now from C you can do it – jmj Feb 15 '11 at 18:14
  • 1
    @Jigar: Yes, but that's clearly *not* what was shown in the question. That's not calling an abstract method - it's calling a *concrete* method implemented in the superclass. – Jon Skeet Feb 15 '11 at 18:21
  • 1
    agree, but when I answered the code was written in C++, and I thought it was just to demonstrate the scenario so I wrote it in that way and then also beaten :) anyways your answer is perfect – jmj Feb 15 '11 at 18:25
  • @Jigar: I wouldn't say it was "written in C++" - it used ":" instead of "extends" but that's all. Everything else was Java-based, including the tag and question title :) – Jon Skeet Feb 15 '11 at 18:26
  • Probably I don't compile C++ perfectly :) anyways +1 – jmj Feb 15 '11 at 18:28
6

When you put 'super.' before the method name, you say to compiler: 'hey man! call the method implemented exactly in the Base class'. It doesn't exist there actually so it cannot be called and compiler complains. Just remove 'super.' and leave 'foo(arg);' only. This which will tell the compiler to look for a implementation in some subclass.

BTW, if condition in your example is always false, it'll get into infinitive loop and crash because of out of memory :)

Cheers, ~r

rposcro
  • 61
  • 1
  • 1
3

That won't compile. You can't invoke an abstract method.

Robby Pond
  • 70,876
  • 16
  • 121
  • 117
2

Abstract method can't be called as it is just a declaration type, without a definition there is no point calling it. Thus Compile time Exception will occur

Piyush Sharma
  • 35
  • 1
  • 9
2

Tossing your example into Eclipse and editing it so it actually compiles that far produces this error:

"Cannot directly invoke the abstract method foo(String) for the type Base"

Are you sure that comes from a "working code base?"

Isaac Truett
  • 8,460
  • 1
  • 25
  • 48