11

In java can an abstract method be anything other than public? Are abstract methods implicitly public or are they package if you don't specify? (regular methods are implicitly package right?) are there any visibility modifiers that an abstract method can't have? (private strikes me as problematic)

David
  • 14,569
  • 34
  • 78
  • 107
  • 1
    http://stackoverflow.com/questions/2874350/cant-declare-an-abstract-method-private – hvgotcodes May 15 '11 at 03:42
  • that does answer the bit about them being private – David May 15 '11 at 03:48
  • 3
    I downvoted because a simple google search found countless definitions of Java abstract methods and what modifiers work. This place shouldn't become a lazy substitute for the vast amount of Java documentation on the net or at Oracle's website – gshauger May 15 '11 at 04:37
  • that sounds like an issue to be discussed on meta not on my question. – David May 15 '11 at 05:31
  • 1
    -1 for asking a question you could check for yourself with the compiler. – user207421 May 15 '11 at 23:35

5 Answers5

18

abstract methods have the same visibility rules as normal methods, except that they cannot be private.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
8

Why don't you just test it?

abstract class A { 
  private   abstract void pri ();
  protected abstract void pro ();
        abstract void pa ();
  public    abstract void pu ();
}

javac A.java 
A.java:2: illegal combination of modifiers: abstract and private
  private   abstract void pri ();
                          ^
1 error

a) Yes, a private abstract method is useless, and makes the whole class useless.

abstract class B { 
//  private abstract void pri ();
  protected abstract void pro ();
        abstract void pa ();
  public    abstract void pu ();
}

public class A extends B {
  protected void pro () {} ;
            void pa () {} ;
  public    void pu () {} ;
}

The other access modifying keywords are all accepted.

user unknown
  • 35,537
  • 11
  • 75
  • 121
1

Default and Protected level visibility are also usable.

Tauf
  • 21
  • 1
0

Default accessibility for abstract method can also be problematic if subclass is in different package, As members with Default accessibility can only be accessed by classes in same package.

And if you can not access inherited abstract class, you will not be able to override it. Example as below.

package PackA;
abstract public class SupClassA
{
    protected abstract void pro();
    abstract void pa();
}

package PackB;
import PackA.*;
class SubclassB extends SupClassA
{
    protected void pro()
    {
        System.out.print("This is implementation of Pro method");
    }   
    void pa()
    {
            System.out.print("This is implementation of Pa method");
    }
}

When compiled, compiler complains that it can not override method pa, even though implementation is provided.(Works when pa() method is declared as protected in both classes)

E:\VrushProgs>javac -d . PackA\SupClassA.java

E:\VrushProgs>javac -d . PackB\SubClassB.java

PackB\SubClassB.java:3: error: SubclassB is not abstract and does not    
override abstract method pa() in SupClassA
class SubclassB extends SupClassA
^
1 error
Vrushali
  • 43
  • 6
0

Only public & abstract are permitted in combination to method.

Example:

public abstract void sum();

We use abstract keyword on method because Abstract methods do not specify a body.

Ali
  • 3,373
  • 5
  • 42
  • 54
Ashish Ranjan
  • 113
  • 1
  • 1
  • 7